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

조회 수: 3 (최근 30일)
Chris Dan
Chris Dan 2019년 11월 9일
편집: Fabio Freschi 2019년 11월 10일
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