0

I am trying to compute the variance of elements which are organised in matrices (in MATLAB). As an example, let's be A and B two matrices 2x2.

enter image description here

My goal is to find the matrix V (2x2 as well), being the variance of each element of A and each element of B, that is:

enter image description here

Can somebody help me on this?

mgiordi
  • 151
  • 8
  • 3
    That's...not really what variance is – sco1 Apr 20 '16 at 16:06
  • Ok, I am just trying to compute the variance of elements which are organised in matrices. I think the example is self-explicative. – mgiordi Apr 20 '16 at 16:09
  • 2
    Finding the variance of two elements in a signal does not provide enough discriminative power to give you any indication in what is going on in your data. Can you tell us your reason as to why you want to do this? – rayryeng Apr 20 '16 at 16:09
  • @rayryeng I have multiple copies of one matrix, and I am trying to compute the variance of each element of the matrix, over time. – mgiordi Apr 20 '16 at 16:12
  • So do you want to compute the variance as new data comes in? Look at computing the rolling variance: http://stackoverflow.com/questions/5147378/rolling-variance-algorithm – rayryeng Apr 20 '16 at 16:13
  • @rayryeng yes, it's something like that, but I should do this element-by-element. I was wondering if there's a computational easy way to to that, when elements are organised in matrices, without the need of designing multiple for loops, to access each element of the matrix, one at a time. – mgiordi Apr 20 '16 at 16:16
  • 1
    @mgiordi ah, then Dev-iL's answer below should help. Simply make a 3D matrix and find the variance of each 2D position temporally. Bear in mind that only have two matrices isn't enough to tell you anything... I'm assuming you will have more than two matrices and so if that's the case, the code below will work. – rayryeng Apr 20 '16 at 16:17

1 Answers1

6

This is a very simple use case of the var function:

A = [1 2;
     3 4];

B = [5 6;
     7 8];

V0 = var(cat(3,A,B),0,3);   
V1 = var(cat(3,A,B),1,3);

This results in:

V0 =

     8     8
     8     8

V1 =

     4     4
     4     4

What happens is that you concatenate your matrices along some unused dimension and then compute the variance along that dimensions.

NOTE: The example of 2 matrices is not very meaningful, but I'm assuming your actual dataset is larger, in which case you could use this method.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • Thank you. Yes, obviously my dataset is actually larger, I was just trying to make it simple to post the question here :) – mgiordi Apr 20 '16 at 16:27