why does the window show me x3=27 when x3=1:2:50?

1 回表示 (過去 30 日間)
yang-En Hsiao
yang-En Hsiao 2019 年 3 月 31 日
編集済み: madhan ravi 2019 年 3 月 31 日
I wrote this code ,and the c3 can be a vector ,the elements in this vector is the result of every loops,however,i found that the window show me an error ,here is my code
XX3=1:2:50
AXX3=zeros(1,length(XX3))
a3=5
b3=-3
for x3=1:2:50
y3=a3*x3^2+b3
c3(x3)=AXX3(x3)+y3
end
And here is the error
Attempted to access AXX3(27); index out of bounds
because numel(AXX3)=25.
Error in wqeqwe (line 7)
c3(x3)=AXX3(x3)+y3
I found that the x3 = 27.i wonder why? x3 is 1:2:50.there should be only 25 elements,i mean ,x3 should be as same as XX3,does anyone know where is my mistake

回答 (1 件)

madhan ravi
madhan ravi 2019 年 3 月 31 日
編集済み: madhan ravi 2019 年 3 月 31 日
No loop's needed:
XX3=1:2:50;
a3=5;
b3=-3;
x3=1:2:50;
y3=a3*x3.^2+b3;
AXX3=zeros(size(XX3));
c3=AXX3+y3; % if AXX3 is zero then why add it with y3 to get c3?
  5 件のコメント
yang-En Hsiao
yang-En Hsiao 2019 年 3 月 31 日
ok i understand,but why will the matlab stop and warn when when the x3 is 27?
madhan ravi
madhan ravi 2019 年 3 月 31 日
編集済み: madhan ravi 2019 年 3 月 31 日
% example
x = 1:25 % 25 elements
x(27) % will throw error why?
% ^^---- there exists no such element in the 27th place in x because x has only 25 elements not 27, get it?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by