Why do I get "Array indices must be positive integers or logical values"?

2 ビュー (過去 30 日間)
Fernando Díaz Hidalgo
Fernando Díaz Hidalgo 2021 年 10 月 11 日
回答済み: Dave B 2021 年 10 月 11 日
clc
clear
R=1;
C=1;
vf=18;
ti=0;
tf=10;
h=0.5;
tao=R*C;
t=ti:h:tf;
vt=vf(1-exp(-t/tao));
tabla=[t' vt']

回答 (2 件)

Star Strider
Star Strider 2021 年 10 月 11 日
MATLAB does not recognise implicit multiplication, so the missing multiplication operator
vt=vf(1-exp(-t/tao));
↑ ← HERE
causes MATLAB to treat the contents of the parentheses as an array index, throwing the error.
This now works —
R=1;
C=1;
vf=18;
ti=0;
tf=10;
h=0.5;
tao=R*C;
t=ti:h:tf;
vt=vf*(1-exp(-t/tao));
tabla=[t' vt']
tabla = 21×2
0 0 0.5000 7.0824 1.0000 11.3782 1.5000 13.9837 2.0000 15.5640 2.5000 16.5225 3.0000 17.1038 3.5000 17.4564 4.0000 17.6703 4.5000 17.8000
.

Dave B
Dave B 2021 年 10 月 11 日
You're getting this message because of the line:
vt=vf(1-exp(-t/tao));
vf is the number 18, or, more precisely it's a matrix of size 1,1 which contains the number 18. This syntax is interpeted as trying to take the value of vt in the index 1-exp(-t/tao), so if vt was [5 9 1] and you wrote vt(2), it would return 9. What do you want to accomplish in this line? If it's multiplication...
vt=vf*(1-exp(-t/tao));

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by