Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
logical array to timesteps

Subject: logical array to timesteps

From: Dave

Date: 15 Mar, 2009 19:48:01

Message: 1 of 6

Hi,

I have light intensity measurements for each half hour for each day for 10 years. I have created a logical array of '0's and '1's, from the hours of light and dark, so that the dark hours are '0's and the light hours. the first period is a dark period, i would like this to be timestep one, and the light period immediately after to be timestep 2, the next dark to be timestep 3 and so on. can someone help me out with this?

Thanks in advance

Dave Yong

Subject: logical array to timesteps

From: Miroslav Balda

Date: 15 Mar, 2009 21:32:00

Message: 2 of 6

"Dave " <yogi_cave@yahoo.com> wrote in message <gpjm1h$db2$1@fred.mathworks.com>...
> Hi,
>
> I have light intensity measurements for each half hour for each day for 10 years. I have created a logical array of '0's and '1's, from the hours of light and dark, so that the dark hours are '0's and the light hours. the first period is a dark period, i would like this to be timestep one, and the light period immediately after to be timestep 2, the next dark to be timestep 3 and so on. can someone help me out with this?
>
> Thanks in advance
>
> Dave Yong

Hi Yong,
I hope that this is a solution of your problem:
Let you have an logical vector of [darkness|light], say:

L = logical([0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0]);
K = abs(diff([1,L]));
K = cumsum(K).*K;

"I" now contains a sequence of 1,0,...,0,2,0,...,0,3,0,........0,end, wher nonzero elements are in places, where there is a switch between darkness and light of the original vector L. Is it what you have wanted?

Mira

Subject: logical array to timesteps

From: Jos

Date: 15 Mar, 2009 21:46:01

Message: 3 of 6

"Dave " <yogi_cave@yahoo.com> wrote in message <gpjm1h$db2$1@fred.mathworks.com>...
> Hi,
>
> I have light intensity measurements for each half hour for each day for 10 years. I have created a logical array of '0's and '1's, from the hours of light and dark, so that the dark hours are '0's and the light hours. the first period is a dark period, i would like this to be timestep one, and the light period immediately after to be timestep 2, the next dark to be timestep 3 and so on. can someone help me out with this?
>
> Thanks in advance
>
> Dave Yong

Does this produce the desired result?

d = [0 1 1 0 1 1 0 0 0 1 1] ; % dark/light measurements
ts = 2*cumsum(diff([0 d])>0)+~d
% ts = [1 2 2 3 4 4 5 5 5 6 6]

hth
Jos

Subject: logical array to timesteps

From: Dave

Date: 15 Mar, 2009 23:30:03

Message: 4 of 6

Yeah! thats great!

I will use a for loop to make averages for each time step! thanks a lot! I'm not sure how it works though. would you mind explaining what the following line actually means? I'd appriciate it a lot, as I am trying to understand what I put in my coursework.

ts = 2*cumsum(diff([0 d])>0)+~d

thanks a lot for your time Jos!

Dave

"Jos " <#10584@fileexchange.com> wrote in message <gpjsup$t88$1@fred.mathworks.com>...
> "Dave " <yogi_cave@yahoo.com> wrote in message <gpjm1h$db2$1@fred.mathworks.com>...
> > Hi,
> >
> > I have light intensity measurements for each half hour for each day for 10 years. I have created a logical array of '0's and '1's, from the hours of light and dark, so that the dark hours are '0's and the light hours. the first period is a dark period, i would like this to be timestep one, and the light period immediately after to be timestep 2, the next dark to be timestep 3 and so on. can someone help me out with this?
> >
> > Thanks in advance
> >
> > Dave Yong
>
> Does this produce the desired result?
>
> d = [0 1 1 0 1 1 0 0 0 1 1] ; % dark/light measurements
> ts = 2*cumsum(diff([0 d])>0)+~d
> % ts = [1 2 2 3 4 4 5 5 5 6 6]
>
> hth
> Jos

Subject: logical array to timesteps

From: Jos

Date: 16 Mar, 2009 07:05:11

Message: 5 of 6

"Dave " <yogi_cave@yahoo.com> wrote in message <gpk31r$ni$1@fred.mathworks.com>...
> Yeah! thats great!
>
> I will use a for loop to make averages for each time step! thanks a lot! I'm not sure how it works though. would you mind explaining what the following line actually means? I'd appriciate it a lot, as I am trying to understand what I put in my coursework.

> > d = [0 1 1 0 1 1 0 0 0 1 1] ; % dark/light measurements
> > ts = 2*cumsum(diff([0 d])>0)+~d

Break down the code into smaller parts

d = [0 1 1 0 1 0 0 0] ;
d1 = [0 d]
d2 = diff(d1) % help diff
d3 = d2 > 0 % logical vector
etc

help cumsum
help not

hth
Jos

Subject: logical array to timesteps

From: Dave

Date: 16 Mar, 2009 08:03:04

Message: 6 of 6

Much appreciated Jos!

Dave

"Jos " <#10584@fileexchange.com> wrote in message <gpktn7$5qu$1@fred.mathworks.com>...
> "Dave " <yogi_cave@yahoo.com> wrote in message <gpk31r$ni$1@fred.mathworks.com>...
> > Yeah! thats great!
> >
> > I will use a for loop to make averages for each time step! thanks a lot! I'm not sure how it works though. would you mind explaining what the following line actually means? I'd appriciate it a lot, as I am trying to understand what I put in my coursework.
>
> > > d = [0 1 1 0 1 1 0 0 0 1 1] ; % dark/light measurements
> > > ts = 2*cumsum(diff([0 d])>0)+~d
>
> Break down the code into smaller parts
>
> d = [0 1 1 0 1 0 0 0] ;
> d1 = [0 d]
> d2 = diff(d1) % help diff
> d3 = d2 > 0 % logical vector
> etc
>
> help cumsum
> help not
>
> hth
> Jos

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us