From: Walter Roberson on
Alan wrote:

> Let say I have a matrix that is 480000 x 1. I would like to have a
> matrix B such that B calculates the mean of every 6 elements
> (1~6,7~12,13~18,...,479995~480000). I am wondering how can I do that?
>
> For the same situation, instead of mean let say I want to do Xth of
> percentile, how can I do that?

By the way: don't expect to be able to get very accurate percentiles -- you
will, after all, be taking the percentile over only 6 elements, so you average
16 2/3 percent gaps between the elements.
From: ImageAnalyst on
Alan:
You can use conv() (it's optimized for speed so don't worry that it
calculates some values you don't need - it's blazingly fast)

% Generate sample data
A = [1:480000]';
N = 6; % per your requirement.
tic;
% Do the convolution
A_Smoothed = conv(A, ones(N, 1)/N);
% Pick out every 6th one.
B = A_Smoothed(N:N:(end-N));
toc;

Elapsed time is 0.028172 seconds.

B =
3.5000
9.5000
15.5000
21.5000
27.5000
33.5000
39.5000
etc.

For the percentiles you need a "rank filter" (that's what you
described is called, at least in the image processing world), which is
not built in to any toolbox that I know of so I think you'd have to
use sort() and cumsum() along with something like nlfilter (image
processing toolbox).

Actually percentile is ambiguous. Do you mean like the 4th element in
size, regardless of what the actual sizes of the 6 elements are? Or
do you mean to calculate percentiles taking into account the value of
the 6 elements. For example, 3 would be the element at the 50th
percentile in this group [1,2,3,100 200 300] if you just go by ranking
them (it's the third biggest out of 6 elements total), but if you take
account the *values* of the elements, the 50th percentile if passed
only when you've reached the 200 element because (1+2+3+100+200) =
306, which is just past the 50% point of the total sum of
(1+2+3+100+200+300) = 606. So clarify what you mean.
-ImageAnalyst
From: Jan Simon on
Dear Alan!

> A = [1;2;3;4;5;6;7;8;9;10;11;12];
> I would like to find out the average of every 6 elements such that it gives:
> [3.5;9.5];

Beside the already mentioned RESHAPE method, it is faster to calculate the MEAN manually with SUM and division:
M = sum(reshape(A, 6, [])) / 6

I've created a C-Mex for calculating the mean of blocks also:
http://www.mathworks.com/matlabcentral/fileexchange/24812

Good luck, Jan
From: tinne123 on
Hey, just disregard my post, from what you wrote I did not understand that you gave a concrete example for something more general.

"tinne123 " <nastanova(a)yahoo.co.uk> wrote in message <hshs0o$amc$1(a)fred.mathworks.com>...
> Hi,
>
> fancy you made a typo in the supposed results, but maybe this is what you wanted:
>
> I = [ mean(A(1:6)); mean(A(7:10))]
> I =
>
> 3.5000
> 8.5000
>
> Good luck!