how to sum cell elements in a dynamic cell table
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
Image Analyst
2022 年 6 月 19 日
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:
採用された回答
Jan
2022 年 6 月 19 日
編集済み: Jan
2022 年 6 月 19 日
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 件のコメント
Jan
2022 年 6 月 19 日
編集済み: Jan
2022 年 6 月 20 日
@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
2022 年 6 月 20 日
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.
その他の回答 (1 件)
Voss
2022 年 6 月 19 日
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]
[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)
disp(result{1})
disp(result{2})
disp(result{3})
4 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!