Use index from max() to extract matching values from another array

Suppose I have 2 n x m arrays A and B and I get the maximum element of each column of A with
[~,I] = max(A)
Now I want to use I to extract the elements of B that correspond to the max element of each column of A.
For example (the example itself is of no particular interest)
A = rand(5)
B = A.^2
[~,I] = max(A)
Now I would like to have a one line command that extracts from B a row vector consisting of, for each j, the I'th element of column j.
Thanks!

 채택된 답변

David Young
David Young 2014년 12월 16일
Bmax = B(sub2ind(size(B), I, 1:size(B,2)))

댓글 수: 4

perfect, thanks very much
Hello,i am facing similar problem. my code is
for w =1:length(mydata)
%[~,I]= find(mydata{w, 5}(:,2)) = find(mydata{w, 1}(:,1));
for o = 1:length(mydata{w,5})
index = find(mydata{w, 5}(:,2) == mydata{w, 1}(:,1)== o);
mydata{w,7} = index;
end
end
i have a cell which contains max and its index values in each row. Now i want to find max index values in another cell and extract those values( matrix 1*2, here i want to extract only 1st col value only). for ex. [M,I] = [2,295]. 295 index value of its max value in certain row, let say (6,26321) is the extracted index value in another cell. extract value is 6(1st col) similarly all other row cells matrix. Thanks in advance.
I'm afraid I can't understand what you are trying to do. Could you augment the code snippet above to make it an MWE. This would require a sample specification of mydata. I also can't see the relationship between your text and the sample code.
Ram
Ram 2018년 7월 29일
편집: Ram 2018년 7월 29일
thanks for responding, attached file as max with its indexand mydata has to find those index values and place it to a new cell. for example, i have index value 690, i am finding in mydata which has (8.1510,110.9065) but i need only first col value only for every index.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

질문:

2014년 12월 16일

편집:

Ram
2018년 7월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by