From: Jakob on
Hi!

I'll give a short description of my situation to more easily pose my question.

I work with alot of 2- and 3-dimensional arrays and often I want to calculate the mean, median and variance of not the whole image, but particular areas.
Suppose we have a 200x200 array A and want to find mean for a 50x50 square in the middle. Easy, mean(mean(A(75:125, 75:125))). For the variance however, this does not work. var(var(A(75:125, 75:125))) would give the variance of the variances over the columns which isn't the same thing as the variance of all the elements. This forces me to do this: B = A(75:125, 75:125); and then var(B(:)) the get the result I want.
To me this seems a bit unnecessary, so basiclly I'm wondering if there is some way to combine the indexing (75:125, 75:125) and (:) without allocating an extra matrix?

I'm sorry for the perhaps unnecessarily long-winded post, but it kind of bugs me when I'm trying to keep memory usage down (my datasets can be quite large), and I couldn't find any info on this using the search...

Best regards
Jakob
From: Matt J on
"Jakob " <jakob.spang.REMOVE.THIS(a)gmail.com> wrote in message <hte0uq$i7n$1(a)fred.mathworks.com>...

This forces me to do this: B = A(75:125, 75:125); and then var(B(:)) the get the result I want.
> To me this seems a bit unnecessary, so basiclly I'm wondering if there is some way to combine the indexing (75:125, 75:125) and (:) without allocating an extra matrix?

=========


Using (:) never allocates an additional matrix. MATLAB is in most cases smart enough to know when no additional data copying is required.

Make sure, incidentally, that you are familiar with the material here:


http://blogs.mathworks.com/loren/2006/05/10/memory-management-for-functions-and-variables/
From: us on
"Jakob " <jakob.spang.REMOVE.THIS(a)gmail.com> wrote in message <hte0uq$i7n$1(a)fred.mathworks.com>...
> Hi!
>
> I'll give a short description of my situation to more easily pose my question.
>
> I work with alot of 2- and 3-dimensional arrays and often I want to calculate the mean, median and variance of not the whole image, but particular areas.
> Suppose we have a 200x200 array A and want to find mean for a 50x50 square in the middle. Easy, mean(mean(A(75:125, 75:125))). For the variance however, this does not work. var(var(A(75:125, 75:125))) would give the variance of the variances over the columns which isn't the same thing as the variance of all the elements. This forces me to do this: B = A(75:125, 75:125); and then var(B(:)) the get the result I want.
> To me this seems a bit unnecessary, so basiclly I'm wondering if there is some way to combine the indexing (75:125, 75:125) and (:) without allocating an extra matrix?
>
> I'm sorry for the perhaps unnecessarily long-winded post, but it kind of bugs me when I'm trying to keep memory usage down (my datasets can be quite large), and I couldn't find any info on this using the search...
>
> Best regards
> Jakob

unfortunately, you cannot contract the command steps, as has been discussed over and over in this NG...

a=magic(4);
a=a(1:2,1:2); % <- this does not work a(1:2,1:2)(:)
a=var(a(:))

us
From: John D'Errico on
"Jakob " <jakob.spang.REMOVE.THIS(a)gmail.com> wrote in message <hte0uq$i7n$1(a)fred.mathworks.com>...
> Hi!
>
> I'll give a short description of my situation to more easily pose my question.
>
> I work with alot of 2- and 3-dimensional arrays and often I want to calculate the mean, median and variance of not the whole image, but particular areas.
> Suppose we have a 200x200 array A and want to find mean for a 50x50 square in the middle. Easy, mean(mean(A(75:125, 75:125))). For the variance however, this does not work. var(var(A(75:125, 75:125))) would give the variance of the variances over the columns which isn't the same thing as the variance of all the elements. This forces me to do this: B = A(75:125, 75:125); and then var(B(:)) the get the result I want.
> To me this seems a bit unnecessary, so basiclly I'm wondering if there is some way to combine the indexing (75:125, 75:125) and (:) without allocating an extra matrix?
>
> I'm sorry for the perhaps unnecessarily long-winded post, but it kind of bugs me when I'm trying to keep memory usage down (my datasets can be quite large), and I couldn't find any info on this using the search...
>
> Best regards
> Jakob

Just use reshape to make the array fragment a column
vector.

John
From: Walter Roberson on
Jakob wrote:
> var(var(A(75:125, 75:125))) would
> give the variance of the variances over the columns which isn't the same
> thing as the variance of all the elements. This forces me to do this: B
> = A(75:125, 75:125); and then var(B(:)) the get the result I want.
> To me this seems a bit unnecessary, so basiclly I'm wondering if there
> is some way to combine the indexing (75:125, 75:125) and (:) without
> allocating an extra matrix?

No. Any way you do it, the matrix is still going to have to be
allocated; the only question is whether it is named or not.

var(reshape(A(75:125, 75:125), [], 1)

is the same thing as doing the two steps.

There is no way internally in matlab to construct an object that somehow
"points to" the multiple memory blocks A(75:125, 75:125) -- non-sparse
numeric arrays must be contiguous in memory, so one way or another
A(75:125, 75:125) would have to be copied into a new block of memory.
The column version of that new block does not, though, necessarily
require more memory to be allocated -- that could in theory be handled
by a new block header giving the new dimensions and the same memory
pointer as for the square block.