How do I find and store the location of the minimum value in each column of a matrix?

6 views (last 30 days)
I have a 401x49903 matrix of densities (called pden). Each column of data is at a different position and the rows are density at the specific position with depth. So I have density values at 401 depths for 49903 locations. The depths are evenly spaced starting from 0 increasing by 10m and ending at 4000m.
The pden matrix has been 'filtered' to contain only densities from 1027.3 to 1027.5 as I'm only looking at one specific water mass. All other densities that didn't fit this requirement have being turned to NaNs. So at some locations no density values fitted this requirement therefore that whole column contains NaNs.
I want to find the shallowest depth at which the minimum density occurs at each location but I'm struggling to get my head around how to do this. I tried a loop and the find function but I couldn't get this to work. Any help would be greatly appreciated. Thank you!

Answers (1)

Stephen23
Stephen23 on 4 Mar 2015
Edited: Stephen23 on 12 Mar 2015
Use min instead of find, and use the dim option to specify that you want to search along the columns:
>> A = [9,1,8;2,7,3;6,5,4];
>> [V,X] = min(A,[],1)
V =
2 1 3
X =
2 1 2
V gives the minimum values, while X gives the indices along that dimension.
  2 Comments
Emma Whettall
Emma Whettall on 4 Mar 2015
Thank you! Just one more question sorry, if there are two minimums in the same column will this give me the indices of both of them or just the first one of them?
Stephen23
Stephen23 on 4 Mar 2015
Edited: Stephen23 on 4 Mar 2015
According the documentation: If there are several identical minimum values, then min returns the index of the first one.
Do you need all of them? One fast way to find all minimum values in each column would be to use bsxfun to test which values are equal to the minimum of each column:
>> B = [9,1,8;2,7,3;6,5,4;0,1,3];
>> [V,X] = min(B,[],1);
>> bsxfun(@eq,B,V)
ans =
0 1 0
0 0 1
0 0 0
1 1 1

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!