finding column # that contains an integer in a matrix

hi,
I want to find indices of columns of a matrix which contains a specific integer.
say magic(5), how can I find column numbers that has got '5' in it if it exists!
thanks,
Mehrdad

 採用された回答

James Tursa
James Tursa 2015 年 7 月 7 日
編集済み: James Tursa 2015 年 7 月 7 日

0 投票

X = your matrix;
indices = find(any(X==5)); % columns that have the number 5 in them

4 件のコメント

mehrdad asadi
mehrdad asadi 2015 年 7 月 7 日
hi James,
thanks it works! I used to use indices = find(X==5) that didn't work and faced with an error that u can't use 'find()' in this way.
thank you anyway,
mehrdad asadi
mehrdad asadi 2015 年 7 月 7 日
another question! what about searching in rows? I just wanna know ;)
James Tursa
James Tursa 2015 年 7 月 7 日
indices = find(any(X==5,2)); % rows that have the number 5 in them
mehrdad asadi
mehrdad asadi 2015 年 7 月 7 日
got it!
thanks

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by