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
1 个评论
darova
2019-10-21
Trying to assign a matrix to (1,1) element?
T = [-4 1 0;1 -4 1;0 1 -4]
%% ...
A(i,j)=T
回答(2 个)
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?
0 个评论
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);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!