Reputation: 37
I have a 3d matrix A of size MxNxZ. I am searching the minimum for each vector in the Z direction like this.
I = min(A(:, :, minInterval : maxInterval), [], 3);
This is working.
Now I want to have variable intervals for each vector. I have then two 2d matrices as follow :
minIntervals of size MxN
maxIntervals of size MxN
I am then trying this :
I = min(A(:, :, minIntervals : maxIntervals), [], 3);
but it did not work and use only minIntervals(1,1) and maxIntervals(1,1).
Do you have an idea, I don't want to use loop because of the size of the data.
Thank you very much.
Upvotes: 2
Views: 89
Reputation: 15867
You can set matrix values that are outside the interval to Inf
and take the min :
In MATLAB r2016b and later:
z=reshape(1:size(A,3),1,1,[]);
A(z<minInterval | z>maxInterval)=Inf;
I=min(A,[],3);
In pre r2016b:
z=reshape(1:size(A,3),1,1,[]);
A(bsxfun(@lt, z, minInterval) | bsxfun(@gt, z,maxInterval))=Inf;
I=min(A,[],3);
Upvotes: 2