Search code examples
arraysmatlabcell-array

matlab create transaction array of 0's and 1's


I have a header cell with 65 elements as such

header = 'id, item1, item2, item3, ..., item64'               

I also have a 10000x1 cell with id's as:

T1  
T2  
T3
...
T10000

And finally I have a 1x10000 cell array with the following. For example:

cell{1} = 'T1' 'Item1' 'Item5'
cell{2} = 'T2' 'Item45'
...
cell{10000} = ...

each of variable length (variable number of items).

I need to create a matrix/array basically with the header in row 1, and each transaction in subsequent rows with a 1 if that item is in the transaction or a 0 otherwise. Like so:

id | item1 | item2 | item3 | ... | item64  
T1 | 0     | 1     | 0       ...
T2 | 1     | 0     | 0       ...

and so on so I would end up with a 65x10000 array

I have tried using the MATLAB function ismember but can't work out how to loop it through the array let alone create the array. I have tried creating an array of zeros but when I try to put each transaction into each row it doesn't work as the strings are of various lengths.

Thanks if anyone can shed some light on how to do this in MATLAB


Solution

  • Here is a simplified example:

    % list of all item strings
    items = strtrim(cellstr(num2str((1:6)','Item%d'))).';
    
    % cell array of individual items for each row
    cells = cell(3,1);
    cells{1} = {'T1' 'Item1' 'Item5'};
    cells{2} = {'T2' 'Item4'};
    cells{3} = {'T3' 'Item2' 'Item3', 'Item4'};
    
    % membership matrix
    M = cell2mat(cellfun(@(c) ismember(items, c(2:end)), cells, 'Uniform',false))
    
    % matching IDs
    ids = cellfun(@(c) c{1}, cells, 'Uniform',false)
    

    The result:

    M =
         1     0     0     0     1     0
         0     0     0     1     0     0
         0     1     1     1     0     0
    
    ids = 
        'T1'
        'T2'
        'T3'