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: Best way to "classify" a vector? ("averaging" subsampling)
Replies: 4   Last Post: Mar 5, 2013 6:05 AM

 Messages: [ Previous | Next ]
 Derek Goring Posts: 3,922 Registered: 12/7/04
Re: Best way to "classify" a vector? ("averaging" subsampling)
Posted: Mar 4, 2013 5:11 PM

On Tuesday, March 5, 2013 10:19:09 AM UTC+13, Luca wrote:
> Hi everyone.
>
> Let's suppose to have these two vectors:
>
>
>
> t=0:0.01:1000; %timestamps
>
> val = rand(size(t)); %value associated with each timestamp
>
>
>
> Now let's suppose I want to calculate the mean value of val in each interval of "t" broad "1".
>
> What would be the best way to do it?
>
> The dumbest would be something _like_ (I'm sure somebody will find a couple of minor errors :-). But try to get the point!):
>
>
>
> t2 = ceil (t);
>
> [t2,I]= unique (t2,'first');
>
> [t2,J]=unique (t2,'last');
>
> meanVal= zeros(max(t2(:),1);
>
> for i=1:numel(meanVal)
>
> meanVal(i) = mean(val(I(i):val(J(i));
>
> end
>
>
>
> Now... I know that matlab always has some built in functions that does all of these basic operations better and faster.
>
> So.... what would be a smarter way to do this??

So, you're doing the equivalent of taking daily means (if the t were Matlab days).
Why is that dumb?

The only thing I'd criticise about your method is the use of i as an index. By default i and j are sqrt(-1). This may seem trivial, but one day this will jump up and bite you. Use something else, like it or jt, for the index.

Date Subject Author
3/4/13 Luca
3/4/13 Curious
3/4/13 Luca
3/4/13 Derek Goring
3/5/13 Luca