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:
Avoid for loop with floor function

Subject: Avoid for loop with floor function

From: Per

Date: 7 Nov, 2011 15:33:30

Message: 1 of 7

Hi
Anyone who has a smart solution to avoid the for loop in this simple program here?

mw: array with measurements
tw: array with time stamps
res: result array summing up measurement results per day

for j=t_start:t_end
        res(j-t_start+1)=sum(mw(floor(tw)==j);
end

Looking forward to your hints.

Subject: Avoid for loop with floor function

From: Roger Stafford

Date: 7 Nov, 2011 21:45:31

Message: 2 of 7

"Per" wrote in message <j98toa$50u$1@newscl01ah.mathworks.com>...
> for j=t_start:t_end
> res(j-t_start+1)=sum(mw(floor(tw)==j);
> end
- - - - - - - -
  Use 'histc'.

Roger Stafford

Subject: Avoid for loop with floor function

From: Per

Date: 8 Nov, 2011 07:15:30

Message: 3 of 7

"Roger Stafford" wrote in message <j99jhr$nkq$1@newscl01ah.mathworks.com>...
> "Per" wrote in message <j98toa$50u$1@newscl01ah.mathworks.com>...
> > for j=t_start:t_end
> > res(j-t_start+1)=sum(mw(floor(tw)==j);
> > end
> - - - - - - - -
> Use 'histc'.
>
> Roger Stafford
---------------------
Thanks for your quick reply Roger.

I have considered using histc, but I need to process the values inside each bin, not only count the number of values. I don't think this can be done with histc.

Subject: Avoid for loop with floor function

From: Bruno Luong

Date: 8 Nov, 2011 07:23:10

Message: 4 of 7

"Per" wrote in message <j9akui$1jb$1@newscl01ah.mathworks.com>...

>
> I have considered using histc, but I need to process the values inside each bin, not only count the number of values. I don't think this can be done with histc.

I could. Usually it can be done by HISTC followed by ACCUMARRAY.

Bruno

Subject: Avoid for loop with floor function

From: Rune Allnor

Date: 8 Nov, 2011 11:03:11

Message: 5 of 7

On 7 Nov, 16:33, "Per " <per.skalb...@gmail.com> wrote:
> Hi
> Anyone who has a smart solution to avoid the for loop in this simple program here?
>
> mw: array with measurements
> tw: array with time stamps
> res: result array summing up measurement results per day
>
> for j=t_start:t_end
>         res(j-t_start+1)=sum(mw(floor(tw)==j);
> end
>
> Looking forward to your hints.

What's the problem with for loops?
Learn how to program instead of messing
with absurdities.

Rune

Subject: Avoid for loop with floor function

From: Per

Date: 8 Nov, 2011 13:10:27

Message: 6 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j9alcu$30l$1@newscl01ah.mathworks.com>...
> "Per" wrote in message <j9akui$1jb$1@newscl01ah.mathworks.com>...
>
> >
> > I have considered using histc, but I need to process the values inside each bin, not only count the number of values. I don't think this can be done with histc.
>
> I could. Usually it can be done by HISTC followed by ACCUMARRAY.
>
> Bruno
Thanks for your hint Bruno - it works now with ACCUMARRAY

Subject: Avoid for loop with floor function

From: Roger Stafford

Date: 8 Nov, 2011 17:44:27

Message: 7 of 7

Rune Allnor <allnor@tele.ntnu.no> wrote in message <d499c96c-95e2-422f-90b1-6bb973e5a491@l24g2000yqm.googlegroups.com>...
> What's the problem with for loops?
> Learn how to program instead of messing
> with absurdities.
- - - - - - - - -
  Rune, the trouble here is with this specific loop. It is highly inefficient in that the "mw(floor(tw)==j" operation must completely scan all of 'tw' for each separate value of j. Some kind of sort operation in needed to avoid this wasteful action.

Roger Stafford

Tags for 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