フィルターのクリア

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 Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by