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 Center および File ExchangeLarge 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