필터 지우기
필터 지우기

How to find which column in a matrix/array has a nonzero value?

조회 수: 1 (최근 30일)
Steven M
Steven M 2017년 4월 26일
댓글: Joseph Cheng 2017년 4월 26일
I am given a matrix:
x = zeros(6,7)
which can have one random nonzero value in the sixth row and I need to be able to detect which column has this nonzero value.
I have tried to use
[row,col] = find(x)
but this does not create a "col" variable that I am able to use in an "if" statement in the function I am writing. Is there a way to find which column has this nonzero value and set the number of the column as a variable. Any help is appreciated.
  댓글 수: 1
Joseph Cheng
Joseph Cheng 2017년 4월 26일
that should be working. to have it clearer can you explain what you're doing with x because
x = zeros(6,7)
x(2,3)=4;
[row col]= find(x)
[row1 col1]= find(x(:))
works even if you accidently made x a 1xN or Nx1 array

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

채택된 답변

James Tursa
James Tursa 2017년 4월 26일
편집: James Tursa 2017년 4월 26일
column_number = find(x(6,:)); % <-- search the 6th row only
However, why didn't the "col" variable in your attempt work for you? It would give the same result as the above.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by