gusl: (Default)
[personal profile] gusl
Array indices start from 1, which means that to properly avoid fencepost errors, your code ends up looking like this:
for day=1:floor(size(p1,2)/24) %for each day
    daytotal(day) = sum(p(24*(day-1)+1:24*(day-1)+24));
end

Lots of extraneous 1s. (And that's because the data is nice: this code can't handle partial days)

I hear NumPy is a very good substitute.

(no subject)

Date: 2008-11-12 06:34 am (UTC)
From: [identity profile] wjl.livejournal.com
seems like this is only a problem if you decide to index everything else from 0.. can't your days start from 1 instead of 0?

(no subject)

Date: 2008-11-12 06:39 am (UTC)
From: [identity profile] gustavolacerda.livejournal.com
They do start from 1. Try implementing a function that tells you which week the n-th day belongs to.

(no subject)

Date: 2008-11-12 06:53 am (UTC)
From: [identity profile] gwillen.livejournal.com
For bonus perversity points, start numbering the weeks at 1.

(no subject)

Date: 2008-11-12 07:18 am (UTC)
From: [identity profile] gustavolacerda.livejournal.com
I am. At least it's consistent.

(no subject)

Date: 2008-11-12 12:37 pm (UTC)
From: [identity profile] altamira16.livejournal.com
Can't 24*(day-1) + 1:24 * (day-1) + 24
= 24 * (day - 1) + 24 + 1:24 * (day-1)
= 24 * (day - 1 + 1) + 1:24 * (day - 1)
= 24 * day + 1:24 * (day - 1)

This dividing by 24 to get your days just to multiply by 24 later is rubbing me the wrong way as well.

Is p different from p1?
You could do something like this, but I am not sure this is any better.
It does eliminate the -1.

for hour = 1:(size(p1,2))
daytotal(floor(hour/24)) = sum(p(hour) + 0:1/24:(1-1/24)*hour);
end

(no subject)

Date: 2008-11-12 03:38 pm (UTC)
From: [identity profile] mapjunkie.livejournal.com
I write a fair amount of Mathematica, where indexing also appears to start from one, but only as an artifact of addressing being at the structural level.

{a, b, c}[[1]] evaluates to a

{a, b, c}[[1]] evaluates to List, because {a, b, c} is actually List[a,b,c]

The result of this means that you can do such strange things as anonymous recursive functions, for example (If[#>1,# #0[#-1],1])& is the factorial function.

(no subject)

Date: 2008-11-12 03:39 pm (UTC)
From: [identity profile] mapjunkie.livejournal.com
I mean

{a, b, c}[[0]] evaluates to List, because {a, b, c} is actually List[a,b,c]

(no subject)

Date: 2008-11-12 06:01 pm (UTC)
From: [identity profile] gustavolacerda.livejournal.com
error! you're trying to write to daytotal(0).

(no subject)

Date: 2008-11-12 06:23 pm (UTC)
From: [identity profile] altamira16.livejournal.com
Replace daytotal(floor(hour/24)) with daytotal(ceil(hour/24)) ?

February 2020

S M T W T F S
      1
2345678
9101112131415
16171819202122
23242526272829

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags