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
Stephen23 2020년 4월 7일

1 개 추천

>> B = find(A.').'
B =
1 2 4 5 6 9 11 12

댓글 수: 4

steve Brian
steve Brian 2020년 4월 7일
And if i want to reset count on each line?
something like this:
A = [100 0 0 2 0; 3 4 0 5 0; 6 0 7 8 9; 0 0 10 11 0; 0 0 0 0 12]
newA= [1 4 1 2 4 1 3 4 5 3 4 5]
Stephen23
Stephen23 2020년 4월 7일
편집: Stephen23 2020년 4월 7일
>> [B,~] = find(A.');
>> B = B.' % optional
B =
1 4 1 2 4 1 3 4 5 3 4 5
Birdman
Birdman 2020년 4월 7일
Good for us to learn :)
steve Brian
steve Brian 2020년 4월 7일
Yeah, thanks a lot :D

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

추가 답변 (2개)

Birdman
Birdman 2020년 4월 7일

0 개 추천

B=reshape(A.',1,[]);
C=(1:numel(A)).*(B~=0);
C=C(C~=0)
Ameer Hamza
Ameer Hamza 2020년 4월 7일
편집: Ameer Hamza 2020년 4월 7일

0 개 추천

new_A = find(A')

댓글 수: 2

steve Brian
steve Brian 2020년 4월 7일
And if i want to reset count on each line?
something like this:
A = [100 0 0 2 0; 3 4 0 5 0; 6 0 7 8 9; 0 0 10 11 0; 0 0 0 0 12]
newA= [1 4 1 2 4 1 3 4 5 3 4 5]
Ameer Hamza
Ameer Hamza 2020년 4월 7일
편집: Ameer Hamza 2020년 4월 7일
The functionality of find function is the same, so
[new_A,~] = find(A');

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

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

태그

질문:

2020년 4월 7일

댓글:

2020년 4월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by