Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
what correction is required in the code?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I am trying to perform 2*2 + 2*5 + 5*8, for [2 2 5 8] vector which is product of consicutive and sum of all. without using sum() & diff() and using for loop
function [out] = pairprodsum (m)
s = 0
for i = 1
b = m(1:end-1).*m(2:end);
s = s + b;
end
out = s;
0 Kommentare
Antworten (2)
James Tursa
am 11 Nov. 2021
If the homework assignment specifies you must use a loop, then I don't think the intent was for you to wrap a simple i=1 range around vectorized code. That being said, I suppose your "loop" technically qualifies.
b is a vector. You simply need to sum all the elements of b and return that. s = s + b does not sum the element of b ... it simply creates a new vector s that is a copy of b. You need different code to sum the elements of b. There are various ways to do this, which I will let you figure out.
4 Kommentare
James Tursa
am 11 Nov. 2021
How could you maybe change the for loop range so it added up all of the elements?
Diese Frage ist geschlossen.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!