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

2 views (last 30 days)
Steven M on 26 Apr 2017
Commented: Joseph Cheng on 26 Apr 2017
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 Comment

Joseph Cheng on 26 Apr 2017
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 on 26 Apr 2017
Edited: James Tursa on 26 Apr 2017
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.