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

F = @(x)sum(cat(3,A{x}),3);
B = cellfun(F,id,'uni',0);
celldisp(B)
B{1} = 1 1 0 0 1 0 0 1 1 B{2} = 1 1 1 0 1 1 1 0 0

댓글을 달려면 로그인하십시오.

 채택된 답변

KSSV
KSSV 2021년 9월 14일
편집: KSSV 2021년 9월 14일
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)
iwant{1} = 1 1 0 iwant{2} = 1 1 1

댓글 수: 3

thank you for your answer, but i realise there was a mistake in my question and now i editted it. Could you please revise your answer if possible? many thanks.
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)
iwant{1} = 1 1 0 0 1 0 0 1 1 iwant{2} = 1 1 1 0 1 1 1 0 0
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2021년 9월 14일

댓글:

2021년 9월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by