Extracting info from multiple matrices

조회 수: 2 (최근 30일)
Robert
Robert 2018년 11월 17일
편집: Stephen23 2019년 8월 20일
Hello,
Wondering if anyone could suggest a simple way to extract info from matrix B based on Matrix A. I have two "correponding" matrices, info in Matrix A relate to info in Matrix B:
A=[1.02 0.2; 2.2 0.4; 0.5 0.6; 4.8 0.5; 1.5 0.3];
B=[58 63 27 1 478];
In this case, I need to identify the maximum value in Matrix A column 2, and then extract the associated info from Matrix B
Maxi=max(A(:,2));
Maxi =0.6, which correspond to 3rd row of Matrix A, and the to 3rd row of Matrix B, or in this case the value = 27
This is a very simple example, as my matrices are very large.
Your suggestions are very appreciated.

채택된 답변

madhan ravi
madhan ravi 2018년 11월 17일
편집: madhan ravi 2018년 11월 17일
A=[1.02 0.2; 2.2 0.4; 0.5 0.6; 4.8 0.5; 1.5 0.3];
B=[58 63 27 1 478];
[value,idx]=max(A(:,2))
B(idx) %corresponding value in B
  댓글 수: 4
M.Prasanna kumar
M.Prasanna kumar 2019년 8월 20일
here only two matrices are there A and B
suppose if there are 10 matrices A, B,C,D,E,F,G,H,IJ and same operation sholud be done as u explaines in the code how to do that?? please help
Stephen23
Stephen23 2019년 8월 20일
편집: Stephen23 2019년 8월 20일
M.Prasanna kumar wrote: "suppose if there are 10 matrices A, B,C,D,E,F,G,H,IJ and same operation sholud be done as u explaines in the code how to do that?"
As always in such situations, the solution is to organize the data in one array, e.g. a cell array, and then trivially use indexing (which is simple and efficient). Here is an example using cellfun, but you could easily use a loop too:
[~,idx] = max(A)
out = cellfun(@(m)m(idx),{B,C,D,E,F,G,H,I,J},'uni',0)
Adjust the dimensions and indexing to suit your arrays.
See also original question from M.Prasanna kumar:

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by