For loop only saving my last run

4 ビュー (過去 30 日間)
Eric
Eric 2019 年 11 月 13 日
回答済み: Jyothis Gireesh 2019 年 11 月 18 日
for i = 1:2
Result(i) = norm(vcu(i,:))
end
p=0
for k = 1:2
ada = vcu(k,:)/Result(k)
end
Where vcu is a vector (a,b,c;a2,b2,c3)
when I run the code I get 2 1x3 vectors as ada but when I want to use ada further in my code it does not work because ada is only saved for k=2
  1 件のコメント
David Hill
David Hill 2019 年 11 月 13 日
ada(k) = vcu(k,:)/Result(k)%I think you just need to index ada.

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

回答 (1 件)

Jyothis Gireesh
Jyothis Gireesh 2019 年 11 月 18 日
Here are a few pointers which may help with your situation:
  • As per the code given above each iteration of the for loop causes the result to be saved in the variable ada, which essentially overwrites the existing value. Since ada is not predefined, it takes the size of the last data assigned to it (1x3).
  • Instead, you may use the following code:
ada = zeros(2, size(vcu, 2));
for k = 1:2
ada(k,:) = vcu(k,:)/Result(k);
end

カテゴリ

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