MATLAB Answers

How to iterate through a Multi-Dimensional Cell array?

59 views (last 30 days)
I have a cell array defined as
A = cell(i,8);
say i = 4. now i am trying to fill up the 4x8 cell array with a function present inside the loop. Say,
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) - 4x8 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.

  0 Comments

Sign in to comment.

Accepted Answer

Sonam Gupta
Sonam Gupta on 6 Dec 2017
In the output that you have shown above, only 4th row is getting updated because value of i is always 4. Another loop is needed to change the value of i from 1 to i as below:
i = 4;
A = cell(i,8);
C = {10 10 10 10};
D = {13 13 13 13};
E = {62 91 71 89};
for j = 1:i
for index=1:8
A{j,index} = zeros(C{j}, D{j}, E{j});
end
end
I hope this helps!

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!