Printing row index or row of a matrix by searching for a particular value?

20 ビュー (過去 30 日間)
Stewart Tan
Stewart Tan 2019 年 8 月 17 日
コメント済み: Stewart Tan 2019 年 8 月 17 日
I have a matrix:
mat = [ 4 8 5 3;
31 4 5 3;
8 3 1 5];
and I want to display the row index or row of the matrix based on a value at the first column.
For example, the first column of the matrix has values 4, 31 and 8. What I'll want to do then is to print the row index or the whole row containing let's say the value 31 at the first column.. Hence the output will be either:
2 %which is the row index containing 31 as the first value
or
31 4 5 3 %the whole row of the matrix
I used the method of ismember():
idx = ismember(A(:,1),31)
But the problem is a logical array is returned, but if the matrix were to be extremely large, i wouldn't want to go through the whole logical array. I plan to use this for checking purposes.

採用された回答

the cyclist
the cyclist 2019 年 8 月 17 日
編集済み: the cyclist 2019 年 8 月 17 日
rowIndex = find(mat(:,1)==31); % Row index
row = mat(mat(:,1)==31,:); % Row
Note that one doesn't need the find command to get the row, because logical indexing will be used.
You could also have gotten it using ismember, as you tried, but swap the arguments:
[~,rowIndex] = ismember(31,mat(:,1));
  1 件のコメント
Stewart Tan
Stewart Tan 2019 年 8 月 17 日
@the cyclist thank you! just what i needed. cheers!

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

その他の回答 (0 件)

カテゴリ

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