How to take the maximum value of an array?

4 views (last 30 days)
Hi, I have this array a = [1:20]. How do I take the maximum value of every 5 elements of it. So I will have b = [5 10 15 20]. Anyone can help me? Thank you.

Accepted Answer

Star Strider
Star Strider on 25 Feb 2015
This works:
a = [1:20];
b = max(reshape(a, 5, []));

Sign in to comment.

More Answers (1)

Greig on 25 Feb 2015
Try something like this...
steps = 5;
b = NaN(mod(length(a)/steps,steps), 1); % preallocate b for speed, if you want to run larger loops
count = 1; % an index count for b in the loop
for ii = 1:steps:length(a)-(steps-1)
inds = ii:ii+(steps-1); % The indices we are interested in
b(count) = max(a(inds)); % Get the max
count = count+1; % add 1 to the counter
  1 Comment
arina octave
arina octave on 25 Feb 2015
hi, thank you for kindly answer my question. but what i want is the x axis of signal b exactly like the x axis of signal a. do you have any idea how to do that? thank you...

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!

Translated by