changing the existing variables on the for loop

I want to change the r value to k value and vice versa for k value at the end of each loop. for example according to the following example, r and k are 4 6 8 10 at the end of first loop but i want k as 6 8 10 12. so after that for the second loop r will add up with 5 and k will add up with 3 and again the variables will change. how can i do this implementation?
abc=[1 3 5 7]
for t=1:3
k=abc+3
r=5+abc
r=k
k=r
end
k = 4 6 8 10
r = 6 8 10 12
r = 4 6 8 10
k = 4 6 8 10
k = 4 6 8 10
r = 6 8 10 12
r = 4 6 8 10
k = 4 6 8 10
k = 4 6 8 10
r = 6 8 10 12
r = 4 6 8 10
k = 4 6 8 10

4 件のコメント

Awais Saeed
Awais Saeed 2021 年 8 月 20 日
It is not clear what you are trying to say.
Kerem Yüksel
Kerem Yüksel 2021 年 8 月 20 日
i have a variable abc. abc goes into k=abc+3 and r=abc+5. now k is = 4 6 8 10 and r is = 6 8 10 12. after the first loop of 'for', i want make the k = 6 8 10 12 and r = 4 6 8 10. for the second loop of 'for', i want to make these mathematical operations with the new values of r and k as follows. k=abc+3 which is expected as = 9 11 13 15 and for r = abc +5, it is expected as = 11 13 15 17. how can i do that?
Kerem Yüksel
Kerem Yüksel 2021 年 8 月 20 日
the change of variables at the end of each round should be done

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

 採用された回答

Stephen23
Stephen23 2021 年 8 月 20 日

0 投票

abc = [1,3,5,7];
k = abc;
r = abc;
for ii = 1:5
[r,k] = deal(k+3,r+5)
end
r = 1×4
4 6 8 10
k = 1×4
6 8 10 12
r = 1×4
9 11 13 15
k = 1×4
9 11 13 15
r = 1×4
12 14 16 18
k = 1×4
14 16 18 20
r = 1×4
17 19 21 23
k = 1×4
17 19 21 23
r = 1×4
20 22 24 26
k = 1×4
22 24 26 28

6 件のコメント

Kerem Yüksel
Kerem Yüksel 2021 年 8 月 23 日
so how can i do these steps with the set of operations that i can not write between parantheses
Stephen23
Stephen23 2021 年 8 月 23 日
@Kerem Yüksel: what "set of operations" do you want to perform?
Kerem Yüksel
Kerem Yüksel 2021 年 8 月 23 日
the variables goes into another 'for' loop on this for loop, 'bitxor' and mixes according to a certain rule. the set of operations are very long to write them there and also i can not write because of similarity issues.
Kerem Yüksel
Kerem Yüksel 2021 年 8 月 23 日
I will be glad if you can help me
Kerem Yüksel
Kerem Yüksel 2021 年 8 月 23 日
also these operations are not all of them, if you can show me how to change variables for every loop in general with these specifications, i will be glad
Stephen23
Stephen23 2021 年 8 月 23 日
編集済み: Stephen23 2021 年 8 月 23 日
@Kerem Yüksel: without more specific details or examples, it sounds like you should use one matrix and indexing.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2021a

質問済み:

2021 年 8 月 20 日

編集済み:

2021 年 8 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by