Array indices must be positive integers or logical values

2 次查看(过去 30 天)
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

采纳的回答

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.

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