sum different cell arrays based on index
1 view (last 30 days)
Show older comments
Hello, i have
A = { [0,1,0; 0,0,0; 0,0,0],...
[1,0,0; 0,0,0; 0,1,1],...
[0,0,0; 0,1,0; 0,0,0],...
[0,0,0; 0,1,0; 0,0,0],...
[0,0,1; 0,0,0; 1,0,0],...
[1,1,0; 0,0,0; 0,0,0],...
[0,0,0; 0,0,1; 0,0,0]} ;
and i = { [1,2,3], [4,5,6,7] }
i need to sum A based on index groups resulting B={ [1,1,0; 0,1,0; 0,1,1], [1,1,1; 0,1,1; 1,0,0] }, how can i do this?
1 Comment
Accepted Answer
KSSV
on 14 Sep 2021
Edited: KSSV
on 14 Sep 2021
A = { [0,1,0], [1,0,0], [0,0,0], [0,0,0], [0,0,1], [1,1,0], [0,0,0]} ;
id = { [1,2,3], [4,5,6] } ;
n = length(id) ;
iwant = cell(n,1) ;
for i = 1:n
iwant{i} = sum(vertcat(A{id{i}})) ;
end
celldisp(iwant)
3 Comments
KSSV
on 14 Sep 2021
The same logic with few changes applies:
A = { [0,1,0; 0,0,0; 0,0,0],...
[1,0,0; 0,0,0; 0,1,1],...
[0,0,0; 0,1,0; 0,0,0],...
[0,0,0; 0,1,0; 0,0,0],...
[0,0,1; 0,0,0; 1,0,0],...
[1,1,0; 0,0,0; 0,0,0],...
[0,0,0; 0,0,1; 0,0,0]} ;
id = { [1,2,3], [4,5,6,7] } ;
n = length(id) ;
iwant = cell(n,1) ;
for i = 1:n
iwant{i} = sum(cat(3,A{id{i}}),3) ;
end
celldisp(iwant)
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!