From: Jakob on
Walter Roberson <roberson(a)hushmail.com> wrote in message <Q%vKn.23609$Ak3.17120(a)newsfe16.iad>...
> 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.

Ok, that is what i thought, just wanted confirmation.
The use of reshape is clever, should have thought of that...

Thank you all for your time!
/Jakob
From: Matt J on
Walter Roberson <roberson(a)hushmail.com> wrote in message <Q%vKn.23609$Ak3.17120(a)newsfe16.iad>...

> 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.
==============

In theory and in practice... The example below shows that the memory pointers (pr and pi) are the same for both matrix shapes.

>> format debug ;
>> A=rand(2)

A =


Structure address = 181171d0
m = 2
n = 2
pr = 19c93500
pi = 0
0.5383 0.0782
0.9961 0.4427

>> B=A(:)

B =


Structure address = 181154f0
m = 4
n = 1
pr = 19c93500
pi = 0
0.5383
0.9961
0.0782
0.4427
From: Matt J on
"Jakob " <jakob.spang(a)gmail.com> wrote in message <hte30g$8mm$1(a)fred.mathworks.com>...

>
> Ok, that is what i thought, just wanted confirmation.
> The use of reshape is clever, should have thought of that...
==========

No, there is no difference between using reshape(x,[],1) and (:).
They have the same effect.
From: Jakob on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <hte3bl$209$1(a)fred.mathworks.com>...
> "Jakob " <jakob.spang(a)gmail.com> wrote in message <hte30g$8mm$1(a)fred.mathworks.com>...
>
> >
> > Ok, that is what i thought, just wanted confirmation.
> > The use of reshape is clever, should have thought of that...
> ==========
>
> No, there is no difference between using reshape(x,[],1) and (:).
> They have the same effect.

I realize that. What I meant by clever is that then I don't have to explicitly allocate a new matrix just to deallocate it soon after. Gives better looking code and is easier to maintain...

Nevertheless, I've learned some new things :)
/Jakob
From: Matt J 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?
==================

If you need to do this repeatedly on blocks extracted from the same image, it might be helpful to use (in a somewhat non-standard way) my Indexbale Function class

http://www.mathworks.com/matlabcentral/fileexchange/26570-direct-indexing-of-function-calls-oop-exercise

For you this would be

A=IndexableFunction(A);
VarianceOf1Block = var(A{75:125, 75:125}(:)); %repeat for other blocks