how to sum cell elements in a dynamic cell table

7 views (last 30 days)
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...
  5 Comments
Akram RAYRI
Akram RAYRI on 19 Jun 2022
I want to get the combination of the rows of the cell matrix regardless what do we have in the cell (scalars or vectors doesn't matter). In this example I give a cell matrix with 3x8 as a dimension.
Image Analyst
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.
save('answers.mat', 'A');
after you read this link:

Sign in to comment.

Accepted Answer

Jan
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
  3 Comments
Jan
Jan on 19 Jun 2022
Edited: Jan on 20 Jun 2022
@Akram RAYRI: I forgot the 1st index:
tmp = tmp + A{r, v(r)}
% ^ inserted in my answer now
% and starting with tmp=0, because it looks simpler
The method works also, if the cell elements are vectors.
Peter Perkins
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.

Sign in to comment.

More Answers (1)

Voss
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
  4 Comments
Akram RAYRI
Akram RAYRI on 19 Jun 2022
Thank you so much dear voss, your solution is working for matrix with integers, now what if we will have a cell matrix, and in each cell we have a vector instead of an integer, and in the end we want as a result a cell matrix that contains all possible sum combinations but this time the sum will be a vector instead of integer.For instance :
A =
3×3 cell array
{3×1 double} {3×1 double} {3×1 double}
{3×1 double} {3×1 double} {3×1 double}
{3×1 double} {3×1 double} {3×1 double}
> A{1,1}
ans =
1
1
1
>> A{2,1}
ans =
1
2
2
>> A{3,1}
ans =
1
3
3

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!