MATLAB: Replacing values in a 4D array.

multidimensional array indexing

This is a question arising from this thread: https://www.mathworks.com/matlabcentral/answers/374453-round-number-closest-to-0-in-array But as it's a distinctly different question to the original post I thought to make another thread. However, if this is an issue please let me know.
I've indexed a 4D array,F, to locate the smallest values in each column of F using:
[~, idx] = min(abs(F(:,:,:,:)));
This gives me an array, 'idx', which contains the POSITIONS of the smallest elements in each column of F. For example,
F = [5 3 6; 4 6 1; 9 5 4] means idx = [2 1 2]. I then tried to replace these identified elements using
F(idx) = 0;
However, it's only replacing the first 5 elements of the first column of F with 0. I think this is because as F is a 4D array, when indexing it the elements are listed in a single column where the element positions are 1,2,3,4,5,6 etc… So the values identified in idx range from 1-5. So I think it's only replacing the first 5 elements of F as defined in the single column, rather than each individual column as I want it to.
So the question is how would I go about replacing the elements in each column of F as identified by idx.

Best Answer

So the question is how would I go about replacing the elements in each column of F as identified by idx.
The elements refered to by idx are only the first occurraence of the minima in each column. If you really want all occurrences replaced do,
F(F==min(F,[],1))=0; %assumes R2016b or higher. Otherwise, use bsxfun