フィルターのクリア

Index Vector in MATLAB

2 ビュー (過去 30 日間)
Justine Schneider
Justine Schneider 2017 年 7 月 31 日
コメント済み: Star Strider 2017 年 7 月 31 日
How do I index a vector in a for loop? I should be able to call the first entry of the vector a with a(1); however, I get an error when I have this as my for requirement. The error says unbalanced or unexpected parenthesis.
Here is a simplified version of my code.
a = [1 2 3]; b = [4 5 6];
for a(1) AND b(1);
suma = a + 1;
sumb = b +1;
end

採用された回答

Star Strider
Star Strider 2017 年 7 月 31 日
I do not understand what you want to do.
One of these should work:
a = [1 2 3]; b = [4 5 6];
for k = 1:min([numel(a) numel(b)])
suma = a(k) + 1;
sumb = b(k) + 1;
end
or:
suma = 0; sumb = 0;
for k = 1:min([numel(a) numel(b)])
suma = a(k) + suma;
sumb = b(k) + sumb;
end
or:
suma = sum(a);
sumb = sum(b);
or maybe something else ...
  3 件のコメント
Justine Schneider
Justine Schneider 2017 年 7 月 31 日
... from your first MATLAB code answer.
Star Strider
Star Strider 2017 年 7 月 31 日
That simply prevents the index from reading a shorter vector beyond the number of elements it has. (Here, they are both the same size.)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by