# how to sum cell elements in a dynamic cell table

4 views (last 30 days)
Akram RAYRI on 18 Jun 2022
Commented: Peter Perkins on 20 Jun 2022
Hi guys, I have a challenging problem here :
A{b,i} is a cell table with b lines and i columns. the problem is that b can be any integer (dynamic variable) while i is a fixed number. The problem is the following : how to sum elements by varying the column index of the first row while maintaining column indexes of other rows constant, and again fix column index of all the rows except second row and so on. Note that in case of try to use for loops to vary column indexes that we will need b for loops to be generated dynamically which is irrelevant. You will find more explanation in the following
for b = 1:L
for i = 1:16
A{b,i} = ones(1,8);
end
end
Question : how to sum table elements columnwise according to how many rows we are having. Example : in case of two rows we will have 16x16 sum values, in case of three rows we will have 16x16x16 sum values, etc...
Image Analyst on 19 Jun 2022
Can you just stop making us guess? And making us have to make up sample data which may or may not be the form you have? Or having to make some MATLAB code to generate the printout of A you gave above?
Simply attach your cell array in a .mat file with the paperclip icon.

Jan on 19 Jun 2022
Edited: Jan on 19 Jun 2022
Maybe:
A = num2cell([1:6; 2:2:12; 3:3:18]); % Some test input
S = size(A); % Size of the input
ini = ones(1, S(1)); % Start of virtual FOR loops
fin = ini * S(2); % Stop of virtual FOR loops
n = numel(ini);
v = ini; % Current index vector
Result = cell(repmat(S(2), 1, S(1)));
for k = 1:numel(Result)
tmp = 0; % [EDITED] Sum over elements from rows of A
for r = 1:numel(v)
tmp = tmp + A{r, v(r)}; % [EDITED]
end
Result{k} = tmp;
for iv = 1:n % Increase index vector:
if v(iv) < fin(iv)
v(iv) = v(iv) + 1;
break; % Stop "for iv" loop
end
v(iv) = ini(iv); % Reset this index
end
end
Peter Perkins on 20 Jun 2022
I have not read this whole thread, but:
Cell arrays are NOT a good way to store scalar numeric values. You are just making your life harder. If there's a reason you want to use a cell array, you should make that clear. Otherwise, don't do that.

### More Answers (1)

Voss on 19 Jun 2022
Is this similar to what you want to do? (Using 5 columns here instead of 16, and using 3 rows.)
data = (1:5).*[1;7;20]
data = 3×5
1 2 3 4 5 7 14 21 28 35 20 40 60 80 100
[L,M] = size(data);
result = repmat({0},1,L);
c_data = num2cell(data.',1);
siz = ones(1,L);
for ii = 1:L
for jj = 1:ii
siz(jj) = M;
result{ii} = result{ii}+reshape(c_data{jj},siz);
siz(jj) = 1;
end
end
disp(result)
{5×1 double} {5×5 double} {5×5×5 double}
disp(result{1})
1 2 3 4 5
disp(result{2})
8 15 22 29 36 9 16 23 30 37 10 17 24 31 38 11 18 25 32 39 12 19 26 33 40
disp(result{3})
(:,:,1) = 28 35 42 49 56 29 36 43 50 57 30 37 44 51 58 31 38 45 52 59 32 39 46 53 60 (:,:,2) = 48 55 62 69 76 49 56 63 70 77 50 57 64 71 78 51 58 65 72 79 52 59 66 73 80 (:,:,3) = 68 75 82 89 96 69 76 83 90 97 70 77 84 91 98 71 78 85 92 99 72 79 86 93 100 (:,:,4) = 88 95 102 109 116 89 96 103 110 117 90 97 104 111 118 91 98 105 112 119 92 99 106 113 120 (:,:,5) = 108 115 122 129 136 109 116 123 130 137 110 117 124 131 138 111 118 125 132 139 112 119 126 133 140
Akram RAYRI on 19 Jun 2022
@Voss did you get the question ?