Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 3-by-3.

조회 수: 1 (최근 30일)
My goal is to have a 9x9 matrix which looks like
T I 0 0 0 0 0 0 0
I T I 0 0 0 0 0 0
0 I T I 0 0 0 0 0
0 0 I T I 0 0 0 0
0 0 0 I T I 0 0 0
0 0 0 0 I T I 0 0
0 0 0 0 0 I T I 0
0 0 0 0 0 0 I T I
0 0 0 0 0 0 0 I T
0 0 0 0 0 0 0 0 T
Its a 2D matrix and not a multideminsional matrix however, the diagonal elements are another matrix. Below is my code
Can someone tell me what is wrong? Logically?
T = [-4 1 0;1 -4 1;0 1 -4]
I = [1 0 0;0 1 0;0 0 1]
A = zeros(9,9)
for i=1:9
for j=1:9
if i==j
A(i,j)=T
elseif j==(i+1)
A(i,j)=I
elseif j==(i-1)
A(i,j)=I
end
end
end

답변 (2개)

Stephan
Stephan 2019년 10월 21일
You could check:
I used this function to build the matrix you expect - as far as i understood you correctly:
T = [-4 1 0;1 -4 1;0 1 -4];
I = [1 0 0;0 1 0;0 0 1];
A = zeros(9,9);
for k = 1:size(T,1):size(A,1)
A = linalg_substitute(A,T,k,k);
end
for k = 1:size(I,1):size(A,1)
A = linalg_substitute(A,I,k,k+size(T,2));
end
for k = size(I,1):size(I,1):size(A,1)
A = linalg_substitute(A,I,k+1,k-size(T,2)+1);
end
which produces:
A =
-4 1 0 1 0 0 0 0 0
1 -4 1 0 1 0 0 0 0
0 1 -4 0 0 1 0 0 0
1 0 0 -4 1 0 1 0 0
0 1 0 1 -4 1 0 1 0
0 0 1 0 1 -4 0 0 1
0 0 0 1 0 0 -4 1 0
0 0 0 0 1 0 1 -4 1
0 0 0 0 0 1 0 1 -4
Is this the desired result?

Andrei Bobrov
Andrei Bobrov 2019년 10월 21일
편집: Andrei Bobrov 2019년 10월 21일
A = kron(diag([1 1],1),I) + kron(diag([1 1],-1),I) + kron(eye(3),T);

카테고리

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