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일
X = your matrix;
indices = find(any(X==5)); % columns that have the number 5 in them

댓글 수: 4

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2015년 7월 7일

댓글:

2015년 7월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by