필터 지우기
필터 지우기

Sum repetition in matrices

조회 수: 1 (최근 30일)
Moe
Moe 2014년 4월 28일
댓글: Star Strider 2014년 4월 28일
Hi everyone,
Suppose I have matrix d1, d2, d3, d4, d5:
d1 = [0 0 0 1;0 0 0 0]; % input data
d2 = [0 0 0 0;1 0 0 0]; % input data
d3 = [0 1 0 0;0 0 0 0]; % input data
d4 = [0 0 0 0;1 0 0 0]; % input data
d5 = [0 0 0 0;0 0 1 0]; % input data
and matrix c:
c = [10 12 14 5;1 4 9 15];
Then I want matrix m to be:
m1=c+d1
m2=c+d1+d2 % In other words, in each iteration, the new matrix d should be summed with previous one (same for rest of calculation)
m3=c+d1+d2+d3
m4=c+d1+d2+d3+d4
m5=c+d1+d2+d3+d4+d5
Can anyone please help me?
Note: matrix d is generated with another loop automatically which is dependent to output of matrix m (sum(sum(m)).
  댓글 수: 2
Image Analyst
Image Analyst 2014년 4월 28일
What's wrong with that way? Why do you need a loop?
Moe
Moe 2014년 4월 28일
@ Image Analyst
Actually, I have over than 10,000 of matrix "d", and each of them will be calculated after calculation each matrix "m" (sort of convergence problem).

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

채택된 답변

Star Strider
Star Strider 2014년 4월 28일
This works:
d(:,:,1) = [0 0 0 1;0 0 0 0];
d(:,:,2) = [0 0 0 0;1 0 0 0];
d(:,:,3) = [0 1 0 0;0 0 0 0];
d(:,:,4) = [0 0 0 0;1 0 0 0];
d(:,:,5) = [0 0 0 0;0 0 1 0];
c = [10 12 14 5;1 4 9 15];
m(:,:,1) = c;
for k1 = 2:size(d,3)+1
m(:,:,k1) = m(:,:,k1-1) + d(:,:,k1-1);
end
m = m(:,:,2:end) % Result for ‘m’
  댓글 수: 2
Moe
Moe 2014년 4월 28일
Thanks Star. It was great!
Star Strider
Star Strider 2014년 4월 28일
My pleasure!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by