Calculate Matrix and then use previously calculated matrix in next iteration
1 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!