Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: counting events
Replies: 4   Last Post: Aug 22, 2013 6:15 PM

 Messages: [ Previous | Next ]
 Jonathan Allan Posts: 123 Registered: 12/7/04
counting events
Posted: Aug 22, 2013 3:45 PM

Hi,

I'm trying to figure out a way to perform a cumulative count of events. Here's my example. I have the following matrix:

test=[0,0,1,1,1,0,0,0,0,0,1,1,0 etc].'; and I am trying to get to the following solution: test2=[0,0,1,2,3,0,0,0,0,0,1,2,0 etc];

Basically, I would like to add those events characterized by a 1, so that the end result is the sum of all the 1s in the sequence.
I started with the following but its not working (I can see why but can't figure out how to get around it)...

for k = 1:length(test)
if test(k)>0
test3(k)=test(k-1)+test2(k);
elseif test(k)==0
test3(k)=0
end
end
test3=test3.';

Thanks

Date Subject Author
8/22/13 Jonathan Allan
8/22/13 Derek Goring
8/22/13 Jonathan Allan
8/22/13 Alan
8/22/13 Alan