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

3 visualizaciones (últimos 30 días)
Chris Dan
Chris Dan el 9 de Nov. de 2019
Editada: Fabio Freschi el 10 de Nov. de 2019
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

Respuestas (1)

Fabio Freschi
Fabio Freschi el 10 de Nov. de 2019
Editada: Fabio Freschi el 10 de Nov. de 2019
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?

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by