Subtracting Current and Previous Value of the same variable
20 ビュー (過去 30 日間)
古いコメントを表示
The current code I have is extremely long but (water distribution analysis), I cant figure out how to loop my code while I havent met a specific condition. I really nned array 'a' to stay the same array name. I just need to update it to a new value while the difference between the new 'a' and old 'a' is less than 1000.
clc
clear all
n_row = 2;
n_col = 2;
%predefined a
a(1,1) = 1;
a(1,2) = 2;
a(2,1) = 3;
a(2,2) = 4;
a
for row = 1:1:n_row;
col = 1:1:n_col;
x(row,col) = 0.01*a(row,col)
end
%%%after calculating 'x' which require the value of 'a', I have to adjust
%%%all values of 'a' by 'x'
a = a + x;
a
%%%without changing variable name is there a way I can subtract the new value of
%%%'a' with its old value. If the difference between the new 'a' and old
%%%'a' is less that 1000 repeat the process using the new value 'a'.
0 件のコメント
回答 (1 件)
Rik
2021 年 7 月 6 日
No,that is not possible. What you can do is create a new variable which contains the previous value of a. Then you can trivially subtract the two.
I hope your code is just an example, because your nested loop is not needed at all.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Annotations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!