Write a function called winmax that receives a vector of real numbers as the fir
ID: 3918518 • Letter: W
Question
Write a function called winmax that receives a vector of real numbers as the first input argument, and a scalar representing the window size as the second input argument. The function should find and return the maximum window sum for the specified window size and vector.
Here are several examples of what calling winmax might look like:
>> winmax([1 8 -3 2 7 4 -5], 1)
ans =
8
>> winmax([1 8 -3 2 7 4 -5], 2)
ans =
11
>> winmax([1 8 -3 2 7 4 -5], 3)
ans =
13
>> winmax([1 8 -3 2 7 4 -5], 4)
ans =
14
Explanation / Answer
function m = winmax(vec, n)
m = sum(vec(1:n));
for i=1:length(vec)-n+1
if sum(vec(i:i+n-1)) > m
m = sum(vec(i:i+n-1));
end
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.