How to delete one element of the third dimen in a 4d matrix?

1 view (last 30 days)
Hi,
i have a 4d Matrix with the size of [i j n k] target size is [i j n-1 k] and i want to delete always the smalest of the n elements. I made it like this:
sz = size(Matrix);
Matrix_neu = zeros(sz - [0 0 1 0]);
[~, I] = min(Matrix,[],3);
for i = 1:sz(1)
for j = 1:sz(2)
for k = 1:sz(4)
temp = true(1,sz(3));
temp(I(i,j,1,k)) = false;
Matrix_neu(i,j,:,k) = Matrix(i,j,temp,k);
end
end
end
but i think there is a more elegant and faster way isn't it?
Regards Markus

Accepted Answer

Matt J
Matt J on 22 May 2014
Edited: Matt J on 23 May 2014
If you know the minima are uniquely attained, you could do something like this,
[m,n,p,q]=size(Matrix);
Matrix = reshape( permute(Matrix,[3,2,1,4]), p,[] );
idx=bsxfun(@gt, Matrix, min(Matrix,[],1));
Matrix_neu=ipermute( reshape( Matrix(idx), [p-1,n,m,q]) , [3 2 1 4]);
  2 Comments
Markus
Markus on 23 May 2014
i think there is a typing error: you wrote:
[3 1 2 4] @ ipermute
and:
[3,2,1,4] @ permute
they must be the same or?
Regards Markus
Matt J
Matt J on 23 May 2014
Edited: Matt J on 23 May 2014
Yes, they must both be [3 2 1 4]. I've fixed it.

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!