what changes can be made in the code?

2 Ansichten (letzte 30 Tage)
Manav Divekar
Manav Divekar am 7 Dez. 2021
Kommentiert: Jon am 7 Dez. 2021
if input : mycumsum( [ 3 2 10 ] ) and i want is the vector sum of sum of 1st and second and so on eg [ 3 5 15 ]
function [out] = mycumsum (m)
add = 0;
for i = 1:length(m)
for j = 1:length(m)+1
a = i(m);
%b = ;
end
end
out = add;
  1 Kommentar
Jon
Jon am 7 Dez. 2021
MATLAB's cumsum will do this for you. Is this a programming excercise where you have to write your own code to do provide a cumulative sum?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

David Hill
David Hill am 7 Dez. 2021
function out = mycumsum (m)
out = m(1);
for i = 2:length(m)
out(i)=out(i-1)+m(i);
end

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by