フィルターのクリア

function to solve a binary matrix?

4 ビュー (過去 30 日間)
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016 年 4 月 1 日
If i have a binary matrix (NxM)
A =[0 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1
0 1 1 0 1 1 1 1 1 1 1 1 0 1 0 1
1 1 0 1 0 0 0 0 0 0 0 1 1 1 1 1
0 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1
0 1 1 1 1 0 1 0 0 1 1 1 1 1 0 1
0 0 1 1 0 1 1 0 1 1 1 0 1 0 0 1
0 1 0 0 1 0 1 1 1 1 0 1 1 1 0 1
0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1];
I need a function to calculate number of 1 and in each row and put it in a new matrix like this
B = [ 1 1 1 1 1 1 1
2 8 1 1
2 1 5
1 3 5
4 1 5 1
2 2 3 1 1
1 1 4 3 1
13
15
5 1 ]
And a third matrix for a column like this
C = [ 1 4 2 4 2 1 2 1 2 1 2 4 1 4 2 7
1 3 2 2 2 1 6 4 5 6 2 3 7 3 1 2
2 3 1 2
3 ]
  • Note when the counter see zero then the counter will be stop and start again for example [0 1 1 0 1 1 1 1 1 ] then the solution will be [2 5]

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 4 月 1 日
A =[0 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1
0 1 1 0 1 1 1 1 1 1 1 1 0 1 0 1
1 1 0 1 0 0 0 0 0 0 0 1 1 1 1 1
0 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1
0 1 1 1 1 0 1 0 0 1 1 1 1 1 0 1
0 0 1 1 0 1 1 0 1 1 1 0 1 0 0 1
0 1 0 0 1 0 1 1 1 1 0 1 1 1 0 1
0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1]
[n,m]=size(A);
B=(m+repmat(1:m,n,1)-A.*cumsum(A,2)).*A;
for k=1:n
a=B(k,B(k,:)~=0);
[~,~,kk]=unique(a);
row1{k,1}=accumarray(kk,1);
end
celldisp(row1)
  4 件のコメント
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016 年 4 月 1 日
Thank you
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016 年 4 月 1 日

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by