Sum with a for loop of each 2 elements
10 次查看(过去 30 天)
显示 更早的评论
Hi All,
I have been trying to get the sum of each two elements: for example:
I have x=[50 200 20], I need to have the output =[250 220]
50+200
then
200+20
and so on...
clear all;
clc;
x=[50 200 20];
%x(1:length(x))=x(length(x):-1:1);
for i=1:length(x)-1
c(i) = sum(x); % c(i)=[250 220]
end
0 个评论
回答(1 个)
Sriram Tadavarty
2020-4-21
编辑:Sriram Tadavarty
2020-4-21
Hi Ali,
Use the following modification to the code:
clear all;
clc;
x=[50 200 20];
for i=2:length(x)
c(i-1) = x(i-1) + x(i);
end
c = movsum(x,2);
c = c(2:end);
Hope this helps.
Regards,
Sriram
2 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!