フィルターのクリア

Simultaneous Changing of Values in For Loop.

2 ビュー (過去 30 日間)
Yavar Hayat
Yavar Hayat 2019 年 4 月 22 日
編集済み: Yavar Hayat 2019 年 4 月 22 日
Can I change two values at once in a for loop or any other way in MATLAB?
As an example:
t=0;
for (i=1:1:3)&(j=2:1:4)
k=i*j;
t=t+k;
end
I know this code is wrong but is there a way to simlateously increament i and j values!
i=1 and j=2
k=2
t=2
then
i=2 and j=3
hence
k=6
and t=2+6=8
like that???

採用された回答

Stephen23
Stephen23 2019 年 4 月 22 日
編集済み: Stephen23 2019 年 4 月 22 日
Usually it is much easier to increment over indices, not data:
t0 = 0;
t = t0;
Iv = 1:1:3;
Jv = 2:1:4;
for x = 1:numel(Iv)
k = Iv(x)*Jv(x);
t = t+k;
end
In your case the loop is entirely superfluous anyway:
t = t0 + cumsum(Iv.*Jv)
  1 件のコメント
Yavar Hayat
Yavar Hayat 2019 年 4 月 22 日
編集済み: Yavar Hayat 2019 年 4 月 22 日
The system I had to solve was much more complex than this. Though this is the solution to the example but cuoldn't be applied directly to my system. Nonetheless, it did definitely help me and did solve my system.
Thank you.

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by