MATLAB: How to return corresponding element in two same same size matrices

MATLABmatricesmatrixmatrix arraymatrix manipulation

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);

Best Answer

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))