Question

[MATLAB] Write a function called hw4_problem1 that takes two inputs: a vector v and a positive...

[MATLAB] Write a function called hw4_problem1 that takes two inputs: a vector v and a positive integer scalar n. You do NOT need to check these assumptions. The function needs to find the n consecutive elements in v whose sum is the maximum. It needs to return the sum and the index of the first of these elements. If there are multiple such n consecutive elements in v, it returns the first one, i.e., the one with the smallest index. Here is an example run:

>> [total ind] = hw4_problem1([1 2 3 4 5 4 3 2 1],3)

total =

13

ind =

4

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:

function [ s,index ] = hw4_problem1( v,n)

s=0;
index=0;
for i=1:length(v)-n
temp = sum(v(i:i+n-1));
if s<temp
s= temp;
index = i;
end
end

end

output:

Add a comment
Know the answer?
Add Answer to:
[MATLAB] Write a function called hw4_problem1 that takes two inputs: a vector v and a positive...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT