Subtracting Current and Previous Value of the same variable

22 views (last 30 days)
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'.

Answers (1)

Rik
Rik on 6 Jul 2021
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.

Community Treasure Hunt

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

Start Hunting!