フィルターのクリア

How to use indices returned from [Y,I] = min(X) when X is a 2D matrix?

3 ビュー (過去 30 日間)
Gabe
Gabe 2015 年 4 月 13 日
コメント済み: Titus Edelhofer 2015 年 4 月 13 日
I'm sure there is an answer here but I'm just not finding the right keywords...
I have a large matrix X, and I want the minimum values from each column. min(X) does exactly what I want.
But how would I use the indices returned in "I"? X(I) does not return the minimum values from min(X). Is there notation to address X columnwise, so that I would get back a row vector that matches min(X)?

回答 (2 件)

Jan
Jan 2015 年 4 月 13 日
編集済み: Jan 2015 年 4 月 13 日
The replied indices are realted to each column. So you need sub2ind to get the linear index to obtain the minimal values:
x = rand(3, 3);
[v, ind] = min(x);
w = x(sub2ind(size(x), ind, 1:size(x, 2))));
  2 件のコメント
Gabe
Gabe 2015 年 4 月 13 日
Thanks! sub2ind was just what I needed.
Titus Edelhofer
Titus Edelhofer 2015 年 4 月 13 日
Hi Gabe,
fine, please go ahead then and mark Jan's answer as "the" answer and the question answered ...
Thanks,
Titus

サインインしてコメントする。


Stephen23
Stephen23 2015 年 4 月 13 日
編集済み: Stephen23 2015 年 4 月 13 日
Although the documentation is not very clear on this, the function min returns indices along the dimension that it is working along. So when you sum along each column, the indices are for the position in that column. You can use sub2ind to convert these indices into linear indices:
>> X = randi(9,3,3)
X =
8 9 7
6 3 4
5 7 6
>> [Y,I] = min(X,[],1)
Y =
5 3 4
I =
3 2 2
>> Z = sub2ind(size(X),I,1:size(X,2))
Z =
3 5 8
>> X(Z)
ans =
5 3 4

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by