Array indices must be positive integers or logical values

조회 수: 2 (최근 30일)
Skydriver
Skydriver 2019년 9월 19일
댓글: Skydriver 2019년 9월 19일
I have a problem to develop my coding. Here is my coding:
Mag=(6.5:0.1:9.5);
beta = bvalue * log(10);
MomentRate = (SM * 1e9)*(A*1e6)*(S/1000); MR=MomentRate;
for i = length(Mag)
N(i) = MR*(1.5-bvalue)*(1-exp(-beta*(M-Mag(i))))/(bv*Mo*exp(-beta(M-Mag(i))));
end
The problem is Array indices must be positive integers or logical values.
Let me how to solve my problem.
Thank you
Muktaf
  댓글 수: 1
Ankit
Ankit 2019년 9월 19일
could you please post the complete code?
bvalue is missing.

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

채택된 답변

Adam
Adam 2019년 9월 19일
편집: Adam 2019년 9월 19일
for i = length(Mag)
doesn't make sense as this will just evaluate to a scalar. I don't see how this would be the source of your error, but it clearly isn't correct. I assume you want
for i = 1:numel(Mag)
Using numel or size with a relevant argument is generally better than using length, but the 1: part is the main change.
As to the error in question, using the debugger is the simplest way to find these.
beta( M-Mag(i) )
looks like the source of the error though. This will likely not evaluate to an integer to index into beta, though I don't know what M is.
Also, the parenthesis are un-necessary in
Mag=(6.5:0.1:9.5);

추가 답변 (2개)

madhan ravi
madhan ravi 2019년 9월 19일
편집: madhan ravi 2019년 9월 19일
beta Operator missing here ( M
Note: Don’t name a variable beta because there is an inbuilt function named beta(). Preallocate N. The loop iterator should run from 1:length(...) (but I prefer numel() over length). Likely the loop is not necessary.
  댓글 수: 2
madhan ravi
madhan ravi 2019년 9월 19일
Without loop it’s simply:
N = MR*(1.5-bvalue)*(1-exp(-Beta*(M-Mag)))./(bv*Mo*exp(-Beta operator missing here (M-Mag)));
Skydriver
Skydriver 2019년 9월 19일
Thank you it works now

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


Skydriver
Skydriver 2019년 9월 19일
Here my complete coding
S = 10; %S=Slip;
A = 2000; %A=Area;
SM = 30; %SM=Shear_Modulus;
bv = 0.8; %bv=bvalue;
M = 9.5; %M=Mmax;
m = 6.5; %m=Mmin;
bw = 0.1; %bw=bwith;
Mo = 2e23; %Mo=MoMax;
Mag=(6.5:0.1:9.5);
beta = bvalue * log(10);
MomentRate = (SM * 1e9)*(A*1e6)*(S/1000); MR=MomentRate;
for i = 1:numel(Mag)
N(i) = MR*(1.5-bvalue)*(1-exp(-beta( M-Mag(i) )))/(bv*Mo*exp(-beta(M-Mag(i))));
end

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by