Say I have an array stack
of dimensions NxMxR. I would like to use logical indexing on a given dimension to update its value, without the use of a temporary variable.
Example using the temporary variable:
for k=1:R
temp=stack(:,:,k);
temp(temp<max(max(temp))/2)=NaN;
cropped(:,:,k)=temp;
end
Note that it would be trivial if the condition was the same for the entire array, but it varies along the R
dimension. bsxfun
would be appropriate if I just wanted to subtract the threshold value in a syntax like:
cropped = bsxfun(@minus, stack, max(max(stack, [], 3))/2);
I find the use of the temporary variable clumsy and can't find a proper way of defining the function for bsxfun
to do what the loop does.
Does anyone has a suggestion?
stack(bsxfun(@minus, stack, max(max(stack))/2) < 0) = NaN;