# Finding values in a matrix

6 views (last 30 days)
HA on 18 Aug 2020
Edited: dpb on 19 Aug 2020
Hello,
I have two matrices, the first is a lon,lat,time (70,17,3480) matrix (A), the second is the spatial minimum of that matrix in time (1x3480) (B).
I want to loop through the 3D matrix with the 1D matrix to ultimatly find the locations of the minimum value from the 1D matrix. So basically find where in the 70x17 section the specific 3480 value is? I hope that makes sense.
I guess it wants to be some kind of loop of-
[c] = find(A==B);
I guess some kind of logical loop would do this better?
Thank you!
dpb on 18 Aug 2020
So, if I follow, the vector B is the minimum across each plane of A?
If that is the case, it would seem one would already have been able to have computed/saved that information when found the minimum much more efficiently than in doing a search/match after the fact.

Michael Soskind on 19 Aug 2020
Hi HA,
As dpb mentioned, if you have these minima calculated by matlab, it is easier to use the computed/saved information in that case. However, another solution is to use the find method, but to match the sizes as needed. For instance, I can make a random set of 3D data (A), find the minima (B), and then find the indices of each of the values. These indices are not recalled as a row and column, but rather as a the linear index of the original three-dimensional array.
% Generating matrix of random values, size is 3x3x10 for simplicity
for i = 1:10
A(:,:,i) = rand(3,3);
end
% Calculating the minima of A, stored in matrix B, and the indices of the minima stored in Idx
[B, Idx] = min(min(A));
% 1x10 array showing all of the minima locations in the 3-D array A
C = find(A == repmat(B,[3,3,1]));
I am not exactly sure if that will work for you, or if you need the exact lat and long separately. If so, then this method probably would not work for you. However, it is an alternative that you might find useful if you really need to search for this at another time compared to calculation.
Best,
Michael
dpb on 19 Aug 2020
Yeah...was hoping the comment would prompt the response to tell/show us how these were done so we could retrieve the locations then directly...it might, in fact, be faster to do that again to get the locations than the search.