Defining a matrix using empty parenthesis

2 ビュー (過去 30 日間)
Zena Assaad
Zena Assaad 2014 年 10 月 22 日
コメント済み: Zena Assaad 2014 年 10 月 23 日
Hi all,
Just a small problem with notation. I have the following for loop:
G = [];
W = [];
W(1) = 77000;
for i = 1:29
G = [G, SFC*Thrust1(i)*(disp(i)/V1(i))]
W = [W(i+1), (W(i)-G(i))]
end
I am getting the following error:
Index exceeds matrix dimensions.
Error in fueloptimal (line 139)
W = [W(i+1), (W(i)-G(i))]
I need to define W(i+1) using the empty parenthesis before the 'for loop', i.e. W = [ ]. Have I done this correctly? Or is the issue coming from somewhere else?
Thanks in advance!

採用された回答

Image Analyst
Image Analyst 2014 年 10 月 22 日
The problem is not only W(i+1). It's W(i). Defining W = [], which is null, does not mean that there is a first element, element #1. There is no element W(1). Null means nothing, not even a first element that is null. So you can't append/prepend anything to it. You can append to W if it's null, but not to W(1) or any other specific index number because they're not there yet.
And of course on the ith iteration there is no (i+1)th element either, because you haven't gotten there yet. When i = 1, why do you expect that a W(2) exists already? It doesn't.
  3 件のコメント
Image Analyst
Image Analyst 2014 年 10 月 23 日
Oh, you're right - it is there. If you did that then there was no need to do W=[] just prior to it.
So the first time through your loop with i=1, you're doing W = [W(2), (W(1)-G(1))] but the problem is, on the first iteration there is no W2 yet!
Zena Assaad
Zena Assaad 2014 年 10 月 23 日
Thank you I see the problem now!

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

その他の回答 (0 件)

カテゴリ

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