Calculate Matrix and then use previously calculated matrix in next iteration

1 view (last 30 days)
I start with a set of values (1x50) and perform an equation. I then want the calculated values to be used in the next iteration of the equation instead of the original values, but I'm getting errors.
This is the code I have tried:
topog = 100:149;
erodedSed = (0.1:0.1:5);
newTopog = topog - erodedSed;
for i = 2:10;
newTopog(i) = newTopog(i-1) - erodedSed;
end;
But I'm getting error:
"Unable to perform assignment because the left and right sides have a different number of elements.
Error in Untitled2 (line 6)
newTopog(i) = newTopog(i-1) - erodedSed"
Basically, instead of using "topog" in every iteration following the first, I want the code to replace topog with the "newTopog" calculated in the iteration before it, and then continue doing the equation for the further steps.
Many thanks for your help!

Accepted Answer

Hernia Baby
Hernia Baby on 27 Feb 2021
Edited: Hernia Baby on 27 Feb 2021
" i " is not the number but index number(line or row).
e.g.1 using for
clear;
topog = 100:149;
erodedSed = (0.1:0.1:5);
newTopog(1,:) = topog - erodedSed;
for i = 2:10
newTopog(i,:) = newTopog(i-1,:) - erodedSed;
end
newTopog(end,:)
e.g.2 using while instead of for (Recommended)
clear;
topog = 100:149;
erodedSed = (0.1:0.1:5);
newTopog = topog - erodedSed;
cnt = 1;
while cnt < 10
newTopog = newTopog - erodedSed;
cnt = cnt + 1;
end
newTopog

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!