# Function like "find" but for lines

10 views (last 30 days)
steve Brian on 7 Apr 2020
Commented: steve Brian on 7 Apr 2020
Hi everyone, I need a function that starts counting every element in an array (each line), and when it finds 0, it can skip it.
ex: A = [6 5 0 1; 3 1 0 0; 12 0 100 20]
newA = [ 1 2 4 5 6 9 11 12]
I was trying something like this:
k = 0;
colind = 1:numel(A);
colind(A!=k)
But it only takes me each column in the matrix.
wrong: newA = [1 2 3 4 5 9 10 12]
I want to count my elements on each line of the matrix. A little help please! :)

Stephen23 on 7 Apr 2020
>> B = find(A.').'
B =
1 2 4 5 6 9 11 12
steve Brian on 7 Apr 2020
Yeah, thanks a lot :D

Birdman on 7 Apr 2020
B=reshape(A.',1,[]);
C=(1:numel(A)).*(B~=0);
C=C(C~=0)

Ameer Hamza on 7 Apr 2020
Edited: Ameer Hamza on 7 Apr 2020
new_A = find(A')
##### 2 CommentsShowHide 1 older comment
Ameer Hamza on 7 Apr 2020
Edited: Ameer Hamza on 7 Apr 2020
The functionality of find function is the same, so
[new_A,~] = find(A');

### Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by