Array indices must be positive integers or logical values

1 vue (au cours des 30 derniers jours)
Skydriver
Skydriver le 19 Sep 2019
Commenté : Skydriver le 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
  1 commentaire
Ankit
Ankit le 19 Sep 2019
could you please post the complete code?
bvalue is missing.

Connectez-vous pour commenter.

Réponse acceptée

Adam
Adam le 19 Sep 2019
Modifié(e) : Adam le 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);

Plus de réponses (2)

madhan ravi
madhan ravi le 19 Sep 2019
Modifié(e) : madhan ravi le 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.
  2 commentaires
madhan ravi
madhan ravi le 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)));
Skydriver
Skydriver le 19 Sep 2019
Thank you it works now

Connectez-vous pour commenter.


Skydriver
Skydriver le 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

Catégories

En savoir plus sur Large Files and Big Data dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by