Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adding varying block of elements in a vector

2 views
Skip to first unread message

Adshak

unread,
Mar 11, 2009, 9:31:15 AM3/11/09
to
Dear ALL,

Kind of stuck thinking about a non- for/while loop logic for this
problem here:


Let us imagine a vector of random 1's and 0's such as:

vector = [ 1 0 0 1 0 1 0 1 1 1 1 1 0 0 0 0 1 1 0 1];

%Let me make it clear that the first element must be 1, while the last
of the vector can ideally be 0 or 1.

%Now I have another parallel vector which is basically defined by

parvect = rand(1,length(vector));

% rand is example, my data could be based on exprnd,normrnd or any
random number.

%Let us for ease of understanding fix one instance of rand in parvect
as parvectexample, so let this be(showing only two decimal places):

parvectexample = [ 0.81 0.90 0.12 0.91 0.63 0.09 0.27 0.54 0.95 0.96
0.15 0.97 0.95 0.48 0.80 0.14 0.42 0.21 0.54 0.14];

% length(parvectexample) = length(vector) = 20

Now what I really want is the following:

1) Look into vector:

a) Find blocks of zeroes, [length of block can be anything from 2 to
inf]
This block is defined by the number of zeroes following a 1 and
untill another one occurs. The block should also include the
preceeding 1 as part of it. SO a block looks like this : 1 0 0 0

b)

For example, in vector, the first block is from vector(1) to vector
(3), the last one is vector(18) to vector(19).

2) Add up all the Zero related elements in parvectexample (including
that related to preceeding 1) for each block , and place the sum in
in the preceeding 1's index position and then drop the zero related
elements.

Let me show the results here:

Input:

So we have vector:

vector = [ 1 0 0 1 0 1 0 1 0 0 0 0 1];

parvectexample = [ 0.81 0.90 0.12 0.91 0.63 0.09 0.27 0.97 0.95 0.48
0.80 0.14 0.42];

Output :

VectorDummyOut = [ 1 1 1 1 1 1 1 1 1 1 1] ;
Resultparvect = [1.83 1.54 0.36 0.54 0.95 0.96 0.15 3.34 0.42 0.75
0.14];

Number of elements in result = 20 - number of zeroes in original
vector i.e 11;

Let me show again what is happening :

Resultparvect(1) = 1.83 because as per what I said above:

For first block of zeroes, sum = parvectexample(1) + parvectexample(2)
+ parvectexample(3) = 0.81 + 0.90 + 0.12 = 1.83
After summing and replacing you drop elements (2) and (3)
corresponding to zeroes both in vector and resultparvect.

Similiarly the next block is vector(4) and vector(5),

So you add up parvectexample(4) and parvectexample(5) and replace
parvect(4) with sum i.e 1.54 and drop parvect(5)

parvect(8 to 11) remain intact as these in vector are just 1s with out
any following zeroes.

Hope my problem definition is clear.

I have been breaking my head to do this addition without the use of
for and while loops since my vector and parvect can contain anywhere
between 1 million to 10 million samples, and I want the vector
operation to be fast.

Thanks to matt, With the help of a previous post I know how I could
mark the blocks i.e

startofblock = strfind(vector, [1 0]);
endofblock = strfind(vector,[0,1]);

Now what I have to figure is to amicably sum the block elements for
each block and get the output as shown in the format above.

Will really appreciate any help towards this.

Thanks,

Adshak.

Adshak

unread,
Mar 11, 2009, 7:47:00 PM3/11/09
to

Any help please?

Siyi

unread,
Mar 11, 2009, 11:23:04 PM3/11/09
to
One of the solutions:

v = [ 1 0 0 1 0 1 0 1 1 1 1 1 0 0 0 0 1 1 0 1]';
p = [ 0.81 0.90 0.12 0.91 0.63 0.09 0.27 0.54 0.95 0.96 0.15 0.97 0.95
0.48 0.80 0.14 0.42 0.21 0.54 0.14]';

r = accumarray(cumsum(v),p)

Adshak

unread,
Mar 12, 2009, 9:07:52 AM3/12/09
to

Simply marvellous! Just one line of code and it did the job while I
was thinking of more complex logics!

Thank you soooo much Siyi!

God bless you!

0 new messages