Hello.
I am trying to make a diagnoal matrix in which the three matrices overlap.
I am attaching a picture which will make things more clear.
I can do it for 2x2, but for 3x3 or more like 12x12. I am having confusion.
Can somebody help me expand the logic of 2x2, that would be very nice
here is the code:
B = [ 1 -1;
-1 1];
C = [ 1 -1;
-1 1];
D = [ 1 -1;
-1 1];
T = zeros(4,4)
d=zeros(2,2,3);
d(:,:,1) = B;
d(:,:,2) = C;
d(:,:,3) = D;
for k = 1:1:3
for i = 1:1:2
for j = 1:1:2
T(i+k-1,j+k-1) = T(i+k-1,j+k-1) + d(i,j,k);
end
end
end

댓글 수: 3

Chris Dan
Chris Dan 2019년 11월 10일
jannat alsaidi
jannat alsaidi 2019년 11월 10일
You can do this,
R=zeros(12,12) W=[1 - 1;-1 1] R(1:2,1:2)=W R(3:4,3:4)=W ... For more help give me the two matrices
Chris Dan
Chris Dan 2019년 11월 10일
The two matrices are
A == [ 1 -1 1;-1 2 -1; 1 -1 1]
B == [ 1 -1 1;-1 2 -1; 1 -1 1]

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

 채택된 답변

jannat alsaidi
jannat alsaidi 2019년 11월 12일

0 개 추천

A(:,:,1) = [1 -1 1;-1 2 -1; 1 -1 1];
A(:,:,3) = [1 -1 1;-1 2 -1; 1 -1 1];
A(:,:,5) = [1 -1 1;-1 2 -1; 1 -1 1];
R=zeros(7,7)
for n=1:2:5
i=n+[0 1 2]
j=i
R(i,j)=R(i,j)+A(:,:,n)
end

추가 답변 (1개)

jannat alsaidi
jannat alsaidi 2019년 11월 10일

0 개 추천

A = [ 1 -1 1;-1 2 -1; 1 -1 1];
%A=B
R=zeros(6,6)
for n=1:3
i=n+[0 1 2]
j=i
R(i,j)=R(i,j)+A
end
R
You can change vector [1 2 3], to choose the elements that you need to add them.

댓글 수: 3

Chris Dan
Chris Dan 2019년 11월 11일
will this work for different matrices?
if the values of A and B are not same?
yes, it work for different matrices, try it,
A(:,:,1) = [1 -1 1;-1 2 -1; 1 -1 1];
A(:,:,2) = [2 -1 3; -2 5 -6; 7 8 9];
A(:,:,3) = [-8 2 4; -7 9 5; 2 -5 8];
R=zeros(5,5)
for n=1:3
i=n+[0 1 2]
j=i
R(i,j)=R(i,j)+A(:,:,n)
end
R
if there is a different size of (A) matrix change the vector [0 1 2] to the size that is needed, like size(A)= 6×6 the vector will be [0 1 2 3 4 5].
Chris Dan
Chris Dan 2019년 11월 11일
편집: Chris Dan 2019년 11월 11일
Hey, I ran ur code.
The answer is not correct
can you please check it
The answer which I should get for
A(:,:,1) = [1 -1 1;-1 2 -1; 1 -1 1];
A(:,:,2) = [1 -1 1;-1 2 -1; 1 -1 1];
A(:,:,3) = [1 -1 1;-1 2 -1; 1 -1 1];
R=zeros(6,6)
for n=1:3
i=n+[0 1 2]
j=i
R(i,j)=R(i,j)+A(:,:,n)
end
is
R =[ 1 -1 1 0 0 0 0;
-1 2 -1 0 0 0 0;
-1 -1 2 -1 -1 0 0;
0 0 -1 2 -1 0 0;
0 0 1 -1 2 -1 -1;
0 0 0 0 -1 2 -1;
0 0 0 0 1 -1 1 ]
BUT I am getting :
1 -1 1 0 0 0
-1 3 -2 1 0 0
1 -2 4 -2 1 0
0 1 -2 3 -1 0
0 0 1 -1 1 0
0 0 0 0 0 0
0 0 0 0 0 0
This is wrong...

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

카테고리

도움말 센터File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

질문:

2019년 11월 10일

답변:

2019년 11월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by