필터 지우기
필터 지우기

for loop involving matrix with 3 indices

조회 수: 1 (최근 30일)
Mingze Yin
Mingze Yin 2022년 1월 9일
편집: VBBV 2022년 1월 9일
Hi,
I am trying to obtain a matrix with 3 indices (represented by m, j, n respectively) via the use of a for loop. The general idea looks something like this:
I first set up the matrix to be x=zeros(M+1,J+1,N+1), where M J N are predefined constants;
I then created a triple for loop that resembles -
for n = 2:1:N+1
for m = 2:1:M+1
for j = 2:1: J+1
x(m,j,n) = ...
end
end
end
When I tried to run the code, however, I got an error message at "x(m,j,n) = ..." that says "Index in position 3 exceeds array bounds. Index must not exceed 1." Can someone enlighten me on why this error message popped out? I was certain that x was set up properly at "x=zeros(M+1,J+1,N+1)". Thanks!
  댓글 수: 5
Mingze Yin
Mingze Yin 2022년 1월 9일
hi sir, I just solved the problem. As u suspected, the issue is indeed in the RHS. Thank you for answering!
VBBV
VBBV 2022년 1월 9일
편집: VBBV 2022년 1월 9일
x=zeros(M+1,J+1,N+1)
Vp1(m+1,j,n)
% ^^ problem is in this matrix

댓글을 달려면 로그인하십시오.

채택된 답변

KSSV
KSSV 2022년 1월 9일
It looks like your RHS matrix is 2D and not 3D. Check your RHS matrix from which you are trying to extract elements.
A = rand(2);
A(2,2,2)
Index in position 3 exceeds array bounds. Index must not exceed 1.
  댓글 수: 1
Mingze Yin
Mingze Yin 2022년 1월 9일
Yes I realised the issue lies in my RHS, thank you for answering!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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