From: mb on
I have a matrix of 1's and 0's.
I want to go through the matrix column for column and find sequences of 1's and also find the length of these sequences.

An example:

Matrix A: [1 0 0 1
1 1 0 0
1 1 0 0
1 0 0 1]

I then want an array like this: B = [2 1 0 1], so for every time a sequence of length n is found I will add 1 at position n in B.

Anyone who know how I can solve this?
From: us on
"mb " <marit.berger(a)student.lu.se> wrote in message <hq3teh$qal$1(a)fred.mathworks.com>...
> I have a matrix of 1's and 0's.
> I want to go through the matrix column for column and find sequences of 1's and also find the length of these sequences.
>
> An example:
>
> Matrix A: [1 0 0 1
> 1 1 0 0
> 1 1 0 0
> 1 0 0 1]
>
> I then want an array like this: B = [2 1 0 1], so for every time a sequence of length n is found I will add 1 at position n in B.
>
> Anyone who know how I can solve this?

i don't see how you get from A to B given your task...
otherwise,

one of the solutions

m=[
1 0 0 1
1 1 0 0
1 1 0 0
1 0 0 1
];
n=histc(sum(m),1:4)
% n = 0 2 0 1

us
From: mb on

>
> i don't see how you get from A to B given your task...
> otherwise,
>
> one of the solutions
>
> m=[
> 1 0 0 1
> 1 1 0 0
> 1 1 0 0
> 1 0 0 1
> ];
> n=histc(sum(m),1:4)
> % n = 0 2 0 1
>
> us

I don't want to take the sum of the columns, cause if it is a sequence of zeros between the ones I want to count the ones seperatly. So in matrix M I have 2 "sequences" with 1 ones, 1 sequence with 2 ones and 1 sequence with 4 ones.

mb
From: Bruno Luong on
% One among solutions:

[m n] = size(A);
B = zeros(m+2,n);
B(2:end-1,:) = A;
B = reshape(B,1,[]);
lgt = findstr(B, [1 0])-findstr(B, [0 1]);
B = accumarray(lgt(:),1,[m 1])'

% Bruno
From: us on
"mb " <marit.berger(a)student.lu.se> wrote in message <hq3vlo$snc$1(a)fred.mathworks.com>...
>
> >
> > i don't see how you get from A to B given your task...
> > otherwise,
> >
> > one of the solutions
> >
> > m=[
> > 1 0 0 1
> > 1 1 0 0
> > 1 1 0 0
> > 1 0 0 1
> > ];
> > n=histc(sum(m),1:4)
> > % n = 0 2 0 1
> >
> > us
>
> I don't want to take the sum of the columns, cause if it is a sequence of zeros between the ones I want to count the ones seperatly. So in matrix M I have 2 "sequences" with 1 ones, 1 sequence with 2 ones and 1 sequence with 4 ones.
>
> mb

well... should have been said at the beginning...
anyhow,

one of the many solutions

m=[
1 0 0 1
1 1 0 0
1 1 0 0
1 0 0 1
];
r=regexp(char([0,m(:).',0]+'0'),'0','split');
n=histc(cellfun(@numel,r),1:4)
% n = 2 1 0 1

us