# Find maxima every n positions in 3D matrix

1 view (last 30 days)
Chris on 11 Sep 2017
Commented: Chris on 11 Sep 2017
Hello! I have a 500x400x120 matrix. I need to get the maximum values of every 12 steps in the 3rd dimension, so to end up with a 500x400x10 matrix. I tried
max(matrix(:,:,1:12:end),[],3)
but it gives me a 500x400 matrix as a result. Any ideas? Thanks in advance!
##### 2 CommentsShowHide 1 older comment
Chris on 11 Sep 2017
Yeah, exactly!

Image Analyst on 11 Sep 2017
Do that 10 times but with the indexes correct:
means = zeros(500, 400, 10);
for k = 1 : 10
z1 = (k - 1) * 12 + 1;
z2 = z1 + 11;
means(:, :, k) = max(matrix(:,:, z1:z2), [], 3);
end
Chris on 11 Sep 2017
Thanks a lot!

Kaushik Lakshminarasimhan on 11 Sep 2017
Here's one with while loop. There's probably a faster way.
stepsize = 12; count = 1; i=1;
while i<=size(mymatrix,3)
mymaxmatrix(:,:,count) = max(mymatrix(:,:,i:i+stepsize-1),[],3);
count = count+1; i=i+stepsize;
end
Chris on 11 Sep 2017
Does the job anyway, thanx!