필터 지우기
필터 지우기

2D array in for loop problem

조회 수: 75 (최근 30일)
Suha Ismail
Suha Ismail 2020년 5월 7일
댓글: Suha Ismail 2020년 5월 7일
N=40
a=0.9
x =zeros(41,41)
for k=0:N
for col = 0:41
for row = 0:41
x(row,col)=a.^(k+k)*1;
end
end
end
What is the problem (error: x(0,_): subscripts must be either integers 1 to (2^31)-1 or logicals)?

채택된 답변

Akihumi
Akihumi 2020년 5월 7일
편집: Akihumi 2020년 5월 7일
Unlike Python, C, C++ and other programming languages, Matlab starts the matrix index with 1 instead of 0.
So just change the first number then it should be working without having error.
N=40
a=0.9
x =zeros(41,41)
for k=1:N
for col = 1:40
for row = 1:40
x(row,col)=a.^(k+k)*1;
end
end
end
However, 1st to 39th iteration of k for loop might be overwritten and x would only show the result of 40th iteration of k for loop... And the final result of x would only be a 40-by-40 matrix filled with the value of 0.9^80=2.1847e-4 for all elements...
Which could be simplifed as
N=40
a=0.9
x=a^(2*N)*ones(N)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by