Search code examples
matlabfunctioncellcell-array

How to perform functions on each cell containing 1x17 cells in itself?


I have a nested cell array 'y2a' of size 1x128 with each cell containing cell array of size 1x17.

For eg: y2a{1,1} is a 1x17 cell array.Similarly y2a{1,2} and so on.

I have to multiply the data in each sub-cell array (ie; y2a{1,1} or y2a{1,2}...etc)using the following formula

  for cells 1-7 
   S=(celldata)*(2^(7-i))
  for cells 8-16
   S=(celldata)*(2^(7-i))

where 'i' is the position of the cell.Since there are only 17 subcells and use only 16 of them the value of i varies between (1,16).

Each nested cell in y2a has a 1 bit binary number as its data.

I want to perform the above function for all the nested cells present in each y2a. I tried the following code for performing this

   Y=y2a{1,1}
   for j=1:1:7
       S1(1,j)=(Y(1,j))*(2^(7-j))
   end
   for k=8:1:16
       S2(1,k)=(Y(1,k)*(2^7-k))     
    end

This seems to work out for one cell, but for doing this for all the cells i'm having trouble in forming the for loop.How should i do it in matlab?


Solution

  • Convert the nested cell array into a 2Dmatrix first by using the following code

    A = reshape(cell2mat([y2a{:}]),[],numel(y2a)).';
    

    You could as well look at the below link for i too had asked a similar thing How to separate data from nested cells?

    Then perform the required action using

    B=num2cell(A);%convert it into a 2D matrix of size 128x17
    
    for i3=1:1:128
    for j = 1:1:7
    S1{i3,j} = (B{i3,j})*(2^(7-j))
    end
    end
    
    for i4=1:1:128
    for k = 8:1:16
    S2{i4,k} = (B{i4,k})*(2^(k-7))
    end
    end
    

    you'll probably get the output