Array indices must be positive integers or logical values.

조회 수: 1 (최근 30일)
Jamie Al
Jamie Al 2021년 3월 1일
댓글: Jamie Al 2021년 3월 1일
I am so confused why would I get this error. One minute the code is running fine and produces plots, the next is no longer running. What is the problem with the function Rho? I cleared my workspace multiple times in case something was stuck there but still I am getting the same error. The code was working fine before.
Error message:
Array indices must be positive integers or logical values.
Error in Rho (line 35)
rho(1) = rho(1)+ q*(x_p(i) - x(end-1))/(dx^2); % rhoj
Error in WeakBeam (line 157)
[rho] = Rho(rhoe, xe, x, dx, qe);
I will be posting the code as well for you to run.

답변 (1개)

Steven Lord
Steven Lord 2021년 3월 1일
If x only has one element, x(end-1) is an attempt to access element 0 of x. Arrays in MATLAB don't have an element 0. Their first element is element 1.
You may also receive this error if x is empty, has zero elements. In that case x(end-1) attempts to access element -1 of x. Arrays in MATLAB also don't have an element -1.
  댓글 수: 1
Jamie Al
Jamie Al 2021년 3월 1일
x is an array of 0:1:101 elements so I don't see why would this be a problem cause here x(end-1) would be 99?

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by