-1

I have a cell array defined as A = cell(i,8);

say i = 4. now i am trying to fill up the 4 x 8 cell array with a function present inside the loop.

Say,

A = cell(i,8);
for index=1:8
A{i,index} = zeros(C{i}, D{i}, E{i});
end

where, the values of C{i}, D{i}, E{i} are

C{i} = [10]    [10]    [10]    [10]
D{I} = [13]    [13]    [13]    [13]
E{I} = [62]    [91]    [71]    [89]

And the contents of the cell are obviously zeros, since i used zeros() but i need this step for some further processing.

Now, i should get the Value of A(Cell array) - 4 x 8 Dimension like below,

10x13x62 double10x13x62 double  10x13x62 double 10x13x62 double 10x13x62 double 10x13x62 double 10x13x62 double 10x13x62 double
10x13x91 double 10x13x91 double 10x13x91 double 10x13x91 double 10x13x91 double 10x13x91 double 10x13x91 double 10x13x91 double
10x13x71 double 10x13x71 double 10x13x71 double 10x13x71 double 10x13x71 double 10x13x71 double 10x13x71 double 10x13x71 double
10x13x89 double 10x13x89 double 10x13x89 double 10x13x89 double 10x13x89 double 10x13x89 double 10x13x89 double 10x13x89 double

Instead i am getting the output like,

[]  []  []  []  []  []  []  []
[]  []  []  []  []  []  []  []
[]  []  []  []  []  []  []  []
10x13x89 double 10x13x89 double 10x13x89 double 10x13x89 double 10x13x89 double 10x13x89 double 10x13x89 double 10x13x89 double

I hope I am missing some simple logic behind the loop and the Cell array, is my initialization of the cell array and the loop is correct? if not, please suggest me to find a solution like i mentioned above.

Thanks.

DanteTheSmith
  • 2,937
  • 1
  • 16
  • 31
  • As you have said yourself that `i` equals `4`. How are you expecting `A{i,index} = zeros(C{i}, D{i}, E{i});` to store values in rows other than row#4? – Sardar Usama Dec 04 '17 at 06:13
  • Oh firstly, thanks for the reply Sardar, i am a bit confused because when i create a `A = cell(i,8);` it creates a `1x8` Cell array, so i was confident that the cell array has been initialized correctly, but i messed up clearly some where in the loop, how should i re-write `A{i,index} = zeros(C{i}, D{i}, E{i});` to get the expected output? can u pls help. – Deepak Eevil Personified Dec 04 '17 at 06:32
  • Thanks much for your valuable information Sardar.. thanks much – Deepak Eevil Personified Dec 04 '17 at 07:02
  • Hi Sardar, sorry to interrupt again, i have changed the code like u said, but all the cells in the `4x8` cell array becomes `10x13x89 double`. instead it should be `10x13xN` and N should take the values of `C{I}, D{I}, E{I}, respectively as i described above, but it is only storing the values of E{i}, kindly suggest. – Deepak Eevil Personified Dec 04 '17 at 07:26

1 Answers1

0

Firs thing A{i,index} = zeros(C{i}, D{i}, E{i}); not correct since zero() not accept array as paramerts.

This code will solve your problem

i=4;
A = cell(i,8);

C{i} = {10,10,10,10};
D{i} = {[13]    [13]    [13]    [13]};
E{i} = {[62]    [91]    [71]    [89]};
for j=1:8
 for index=1:4
    A{index,j} = zeros(cell2mat(C{i}(index)), cell2mat(D{i}(index)), cell2mat(E{i}(index)));        
 end
end

This is the output

A =

  4×8 cell array

  Columns 1 through 6

    [10×13×62 double]    [10×13×62 double]    [10×13×62 double]    [10×13×62 double]    [10×13×62 double]    [10×13×62 double]
    [10×13×91 double]    [10×13×91 double]    [10×13×91 double]    [10×13×91 double]    [10×13×91 double]    [10×13×91 double]
    [10×13×71 double]    [10×13×71 double]    [10×13×71 double]    [10×13×71 double]    [10×13×71 double]    [10×13×71 double]
    [10×13×89 double]    [10×13×89 double]    [10×13×89 double]    [10×13×89 double]    [10×13×89 double]    [10×13×89 double]

  Columns 7 through 8

    [10×13×62 double]    [10×13×62 double]
    [10×13×91 double]    [10×13×91 double]
    [10×13×71 double]    [10×13×71 double]
    [10×13×89 double]    [10×13×89 double]
Mohammad nagdawi
  • 553
  • 4
  • 18
  • Hi Nagdawi, i tried it with above logic u have given, but it is throwing error as `Cell contents reference from a non-cell array object`, FYI this piece of code i have provided is already inside the loop of `for i = 1:nfiles`, so, i hope it is not necessary to add the `for index=1:4` loop u have provided. so i just re-wrote the line of code as `A{i,J}=zeros(cell2mat(C{i}(i)), cell2mat(D{i}(i)), cell2mat(E{i}(i)));` and the value of `C{I}, D{I}, E{I} Is dynamic, it changes for every file i.e `for i = 1:nfiles`. Kindly provide ur suggestion on the above. – Deepak Eevil Personified Dec 04 '17 at 10:39
  • you replace `for index=1:4` with what? and why? I write this code just to show you how to make same output you attach to your question. From my ans you can influence your logic according to your problem. – Mohammad nagdawi Dec 05 '17 at 01:39
  • Thank you so much Nagdawi, it worked :) ...i very much appreciate your help – Deepak Eevil Personified Dec 06 '17 at 04:18