calculate the averages and standard deviations of massive matrix
Show older comments
Hi, everyone. I confront with a problem and hope to get help from you. Now I have a massive matrix, 1000*1000 and I would like to calculate the changes of the averages and standard deviations of the matrix as the dimension goes up. The simple but time-comsuming way is easy. First,I can calculate the matrix of 1000*1000, 500*500,250*250,...... then the averages and standard deviations of each matrix can be calculated. I wonder, however, can anyone help me with a new way to calculate the changes of averages and deviations as the dimension goes up, a method that can calculate with several code, rather than so much repeated work.
8 Comments
Walter Roberson
on 5 Jan 2016
Are the smaller matrices always the upper-left portion of the larger matrix? Or is this to be done block by block with different block sizes?
Genhou
on 5 Jan 2016
Image Analyst
on 5 Jan 2016
Edited: Image Analyst
on 5 Jan 2016
It's funny how you think an array only 3% the size of an ordinary digital camera image is "massive" but anyway,....I'm still trying to figure out if you want to use different size input arrays, or if you want just a single largest matrix and you just compute the mean and stddev in a block-by-block fashion with blockproc() with different size blocks. Or if you don't want to move in "jumps" of block sizes and just move an element at a time with conv2() and stdfilt(). Which is it?
Genhou
on 5 Jan 2016
Walter Roberson
on 5 Jan 2016
Are the blocks each half the size of the previous blocksize? If so then if you start with the smallest block you can easily calculate the values for the larger blocks.
Image Analyst
on 5 Jan 2016
Genhou, I won't help until you answer our questions because I don't know what help to offer. There are several possible ways to answer your ambiguous question and I want to make sure I give only the one answer you want.
Walter Roberson
on 5 Jan 2016
Suppose you start with a 1000x1000 matrix and take mean and std over the entire matrix,
m1000 = mean(Array1000(:));
s1000 = std2(Array1000(:));
and then you construct a 500 x 500 matrix of 2 x 2 means:
t500 = conv2(double(Array1000), ones(2,2)/4, 'same');
Array500 = t500(1:2:end, 1:2:end);
and then you take the mean of that:
m500 = mean(Array500(:));
then mathematically this must be the same mean as m1000, to within roundoff error. (The exception would be if you rounded or truncated the fractions when you constructed the 500 x 500 version, then the mean could be slightly different due to loss of precision.)
Answers (1)
Guillaume
on 5 Jan 2016
Well, since the smaller matrices are generating by taking the mean of blocks of the larger matrix, the mean of the whole matrices is never going to change. I'm not sure that there is a way to calculate the standard deviation of the small matrices without generating them but if there is, I'm not convinced that it's going to be faster than generating the smaller matrix in the first place.
I'm not sure what your 'simple but time-comsuming way' is, but the following is simple and shouldn't be too time-consuming:
%reduce a matrix size by averaging block of 2x2:
m = rand(1000); %demo data
assert(all(mod(size(m), 2) == 0), 'cannot create 2x2 blocks since matrix has odd size')
smallm = cellfun(@(mm) mean(mm(:)), mat2cell(m, 2 * ones(1, size(m, 1) / 2), 2 * ones(1, size(m, 2) / 2)));
smallstd = std(smallm(:));
Categories
Find more on Neighborhood and Block Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!