Calculate Matrix and then use previously calculated matrix in next iteration

1 回表示 (過去 30 日間)
Alfie Mackie
Alfie Mackie 2021 年 2 月 27 日
編集済み: Jan 2021 年 2 月 27 日
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!

採用された回答

Hernia Baby
Hernia Baby 2021 年 2 月 27 日
編集済み: Hernia Baby 2021 年 2 月 27 日
" 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
  2 件のコメント
Alfie Mackie
Alfie Mackie 2021 年 2 月 27 日
Thank you so much! That works perfectly!
Hernia Baby
Hernia Baby 2021 年 2 月 27 日
My pleasure!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by