필터 지우기
필터 지우기

Length's of matrix rows exclusing padding

조회 수: 4 (최근 30일)
Nick Keepfer
Nick Keepfer 2018년 3월 1일
댓글: Nick Keepfer 2018년 3월 2일
Hi, I have a matrix which looks a little like
X = [ 0 1 0 1 0 1 1 0 ]
[ 0 1 0 1 1 1 0 0 ]
[ 0 1 1 1 1 0 0 0 ]
This matrix has hundreds of rows so I cant do this manually but I wish to find the length of each vector in the array between the first 1 and the last 1 in each row
In this example it would be :
Y = [ 6 ]
[ 5 ]
[ 4 ]
Can somebody suggest how to do this.
Please note that I have simplified this problem and the numbers I have are not actually all 1's but vary between 1-10 ish.
Thanks :-)

채택된 답변

Akira Agata
Akira Agata 2018년 3월 2일
If each row in X has at least one non-zero value, the following code can calculate Y.
X = [ 0 1 0 1 0 1 1 0;...
0 1 0 1 1 1 0 0;...
0 1 1 1 1 0 0 0 ];
[r,c] = find(X);
Y = splitapply(@(x) max(x)-min(x)+1, c, r);
  댓글 수: 1
Nick Keepfer
Nick Keepfer 2018년 3월 2일
Wow, lightning fast answer!
Works perfectly, thank you very much Akira, you're awesome!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by