Search code examples
matlabindexingdoublecellcell-array

Indexing for a double in cell array matlab


I have a cell array called Event of 179X59 entries and each element is a <1X14> double

so for example Event{1,1} is a 14 bit binary number in it.. like 0 1 0 0 0 0 0 0 0 0 1 0 0 0 which is spread out through columns 1 to 14 so each columns has a bit.

Now my task is to index through each element of the cell array and get into the double and assign a particular alphabet say a for the first bit if I see 1.

So if my alphabets are A through N for a binary 1 1 1 1 1 1 1 1 1 1 1 1 1 1, need to get

A B C D E F G H I J K L M N instead of that binary number.

So for the example give in the second line 0 1 0 0 0 0 0 0 0 0 1 0 0 0 I need to get

0 B 0 0 0 0 0 0 0 0 K 0 0 0

and in the end should return B,K removing zeros.

I have tried assigning each element to a matrix and tried using strrep but its not helping me out.


Solution

  • Code

    cellfun(@(x) cellstr(char(nonzeros(x.*[65:65+numel(x)-1])))', Event,'uni',0)
    

    Results

    For a sample Event:

    Event = {
        [0 1 0 1 0 0 1] [0 1 0 1 0];
        [0 1  1 1 0] [0 0 0 1 1 1 0 0 0 0 1 1 0 1]}
    

    Output -

    >> Event{:}
    ans =
         0     1     0     1     0     0     1
    ans =
         0     1     1     1     0
    ans =
         0     1     0     1     0
    ans =
         0     0     0     1     1     1
    >> out{:}
    ans = 
        'B'    'D'    'G'
    ans = 
        'B'    'C'    'D'
    ans = 
        'B'    'D'
    ans = 
        'D'    'E'    'F'
    

    Of course, the number of columns and rows in Event at its "upper level" are maintained with out -

    >> Event
    Event = 
        [1x7 double]    [1x5  double]
        [1x5 double]    [1x14 double]
    >> out
    out = 
        {1x3 cell}    {1x2 cell}
        {1x3 cell}    {1x6 cell}