Making a 3D Matrix and Adding Matrices to make a bigger matrix

4 次查看(过去 30 天)
Hello Guys, I have a small problem with the error in my for loop.
I am making a 3D matrix and afterwards using nested for loops to make a bigger matrix. here is my code. It is giving me the error
" Index in position 3 is invalid. Array indices must be positive integers or logical values."
I am writing my code:
B = [ 1 -1;
-1 1];
C = [ 2 -2;
-2 2];
D = [ 3 -3;
-3 3];
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) = d(i,j,k-1);
end
end
end

回答(1 个)

Fabio Freschi
Fabio Freschi 2019-11-10
编辑:Fabio Freschi 2019-11-10
In the loop, you wrote
T(i+k-1,j+k-1) = d(i,j,k-1);
k starts from one, so you are trying to access the d(i,j,0) element that does not exists.
What is the expected output you want?

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by