adding matrices inside cell array

조회 수: 16 (최근 30일)
Franco
Franco 2011년 2월 10일
how does one add all of the matrices stores in a cell array?
  댓글 수: 1
Oleg Komarov
Oleg Komarov 2011년 2월 10일
Be more specific, what do you intend by add the matrices? (take the sum of each single matrix, sum elementwise all the matrixes, all the single matrix sums are added together?

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

채택된 답변

vish
vish 2011년 2월 10일
a={[1,2;3,4],[1,2;3,4];[1,2;3,4],[1,2;3,4]}
a{1}+a{2}+a{3}+a{4}
If there are many elements, use two for loops and run all the positions one by one using the equation c=a{i,j} + a{i+1,j+1}.

추가 답변 (1개)

Oleg Komarov
Oleg Komarov 2011년 2월 10일
An example:
% Create dummy input
c = {[1 2; 3 4], [3 3; 1 1]}
c =
[2x2 double] [2x2 double]
% Elementwise sum across matrices (only if ALL matrices have same size)
sum(cat(3,c{:}),3)
ans =
4 5
4 5
% Total sum of each matrix
cellfun(@(x)sum(x(:)),c,'un',0)
ans =
[10] [8]
% Grand sum
c = cellfun(@(x) x(:),c,'un',0);
sum(cat(1,c{:}))
ans =
18
Oleg
  댓글 수: 1
Mariacarla Memeo
Mariacarla Memeo 2011년 9월 30일
Excuse me Oleg,
can you explain your solution for the first case(sum of each element of matrices), because I have to sum 72 matrices [25x25] placed in a cell array [72x1] and I've no idea to how set your solution in my example. Please.

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by