How to find out zeros and their indices in a matrix?

2 ビュー (過去 30 日間)
SM
SM 2020 年 7 月 21 日
コメント済み: SM 2020 年 7 月 21 日
I have an input matrix of size 3*13 double:
input=[0 0 1 0 0 0 1 1 1 0 0 0 0;
0 1 0 0 1 1 0 0 0 1 0 1 0;
0 1 1 1 1 0 0 0 1 0 0 1 0];
I want to know how many zeros subsequently have in
input(2,:)
The output should be:
output=[1 2 3 1 1];
I also want to know their starting index of zeros. It should look like:
index=[1 3 7 11 13];
How can i solve this problem?

採用された回答

Bruno Luong
Bruno Luong 2020 年 7 月 21 日
編集済み: Bruno Luong 2020 年 7 月 21 日
v=input(2,:)>0; % remove >0 if your matrix contains only 0 or 1
d = diff([1, v, 1]);
index = find(d==-1)
output = find(d==1)-index
  1 件のコメント
SM
SM 2020 年 7 月 21 日
Thank you very much!

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2020 年 7 月 21 日
編集済み: Matt J 2020 年 7 月 21 日
One way is to use this FEX submission,
H=group1s(~input(2,:));
output=histcounts(H); output(1)=[];
[~,index]=unique(H); index(1)=[];
  1 件のコメント
SM
SM 2020 年 7 月 21 日
Thank you, Matt!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by