Finding values in a matrix

조회 수: 3 (최근 30일)
HA
HA 2020년 8월 18일
편집: dpb 2020년 8월 19일
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!
  댓글 수: 1
dpb
dpb 2020년 8월 18일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Michael Soskind
Michael Soskind 2020년 8월 19일
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
  댓글 수: 1
dpb
dpb 2020년 8월 19일
편집: dpb 2020년 8월 19일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by