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

 Akzeptierte Antwort

James Tursa
James Tursa am 7 Jul. 2015
Bearbeitet: James Tursa am 7 Jul. 2015
X = your matrix;
indices = find(any(X==5)); % columns that have the number 5 in them

4 Kommentare

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,
another question! what about searching in rows? I just wanna know ;)
indices = find(any(X==5,2)); % rows that have the number 5 in them
got it!
thanks

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by