How to return corresponding element in two same same size matrices

I have two 360*360 matrices (x,y). I want to find the minimum value in each column of the y matrix (YMin - 1*360) and then find the values of the corresponding elements in the x matrix (XMin - 1*360). I tried to use the row numbers but I'm missing something simple here;
[YMin, YRow] = min(y);
XMin = x(YRow);

 채택된 답변

Guillaume
Guillaume 2018년 11월 8일
Your x(YRow) doesn't work because matlab doesn't know that you want element YRow(1) to be taken in the first column, YRow(2) in the second column, etc. One way to solve this:
[YMin, YRow] = min(y);
XMin = x(sub2ind(size(x), YRow, 1:numel(YRow))
Or you can do the calculation that sub2ind does yourself:
XMin = x(Yrow + (0:numel(YRow)-1) * size(x, 1))

추가 답변 (1개)

madhan ravi
madhan ravi 2018년 11월 8일
편집: madhan ravi 2018년 11월 8일
a=rand(4) %fake datas as an example
b=rand(4)
[values,indices]=min(a)
b(indices)=a(indices) %corresponding elements are replaced in second matrix b

댓글 수: 2

madhan ravi
madhan ravi 2018년 11월 8일
편집: madhan ravi 2018년 11월 8일
Then find the values of the corresponding elements
Need some explanation here
you want to returns the corresponding linear index or row?
Give a short example of your output using 2 by 2 matrix
see edited answer
XMin = x(YRow) %returns a 1*360 matrix
XMin = x(YRow,:) %returns a 360*360 matrix
I want to return a 1*360. First, the minimum element in each column and then return value in the second matrix in the same location in each column.
For example if the minimum value in column 1 of the first matrix is (179:1), then i want to return the value in (179:1) in the second matrix, for all columns.
I dont want to replace the elements in the second matrix, i want to return them into a 1*360 matrix (360 values to correspond)
y = [1,2,3:3,2,1:2,1,3];
x = [3,2,1:1,2,3:2,3,1];
[YMin, YRow] = min(y);
XMin = x(YRow); %returns [3 2 1] instead of [3 3 3]

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

릴리스

R2015a

질문:

2018년 11월 8일

답변:

2018년 11월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by