Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote