フィルターのクリア

Counting subrows in each row of a matrix?

3 ビュー (過去 30 日間)
MRC
MRC 2014 年 5 月 7 日
コメント済み: Cedric 2014 年 5 月 7 日
Hi all, I need an algorithm which counts how many adjacent and non-overlapping (1,1) I have in each row of a matrix A mx(n*2) without using loops. E.g.
A=[1 1 1 0 1 1 0 0 0 1; 1 0 1 1 1 1 0 0 1 1] %m=2, n=5
Then I want
B=[2;3] %mx1
In fact A=[(1 1) (1 0) (1 1) (0 0) (0 1); (1 0) (1 1) (1 1) (0 0) (1 1)]. Then, according to this separation, I have 2 (1 1) in the first row and 3 (1 1) in the second row.
  1 件のコメント
Cedric
Cedric 2014 年 5 月 7 日
編集済み: Cedric 2014 年 5 月 7 日
And if one row was
0 1 1 0 0 0
would you count this as
(0 1) (1 0) ..
or still spot the 1 1 block?

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

採用された回答

Cedric
Cedric 2014 年 5 月 7 日
編集済み: Cedric 2014 年 5 月 7 日
Assuming that n is defined previously as
>> n = 5 ;
(for the case of your example) here is a one liner
>> B = sum( reshape( all( reshape( A.', 2, [] )), n, [] )).'
B =
2
3
  2 件のコメント
MRC
MRC 2014 年 5 月 7 日
What if instead (1,1) I want (0,1)?
Cedric
Cedric 2014 年 5 月 7 日
What is the purpose?

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 5 月 7 日
I think you need to use at least one loop
A=[1 1 1 0 1 1 0 0 0 1; 1 0 1 1 1 1 0 0 1 1]
for k=1:size(A,1)
a=[0 A(k,:) 0];
ii=strfind(a,[1 0])-strfind(a,[0 1]);
jj=mod(ii,2)~=0;
ii(jj)=ii(jj)-1;
out(k)=sum(ii)/2;
end
disp(out)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by