## Array indices must be positive integers or logical values

on 19 Sep 2019
Latest activity Commented on by Akhmad Muktaf

on 19 Sep 2019

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

Ankit

### Ankit (view profile)

on 19 Sep 2019
could you please post the complete code?
bvalue is missing.

### Tags

on 19 Sep 2019

on 19 Sep 2019

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);

on 19 Sep 2019

on 19 Sep 2019

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.

on 19 Sep 2019
Without loop it’s simply:
N = MR*(1.5-bvalue)*(1-exp(-Beta*(M-Mag)))./(bv*Mo*exp(-Beta operator missing here (M-Mag)));

on 19 Sep 2019
Thank you it works now

on 19 Sep 2019

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