# Covariance of a P x M x N matrix

I have a stack of tiff files. When imported to Mathematica, they form a $P \times M \times N$ matrix, where $P$ is the number of single images in the stack, $M$ is the number of horizontal pixels and $N$ vertical pixels. I want to calculate the covariance of each vector $P_{mn}$ with the rest of the vectors, where $m$ and $n$ go from 1 to $M$ and $N$ respectively. In the end, I should have $M*N$ matrices with dimension $M \times N$. To get an idea of the data size, I will give some numbers $P\geq50 000$, $M=N=20$.

-
You have been an user for 9 months and still don't know how to properly format your question ?! I don't believe this .. –  Sektor Nov 17 '13 at 16:12
@NikolaDimitrov Well, I have difficulties with the formatting of this site. I am sorry, it is not intentional. Could you tell me what I am doing wrong? –  phidelio Nov 17 '13 at 16:15
My words have been harsh, truth, but there's a help centre which outlines the good formatting practices. –  Sektor Nov 17 '13 at 16:22
What is your aim? To find out how much one image matches another image? Kindly explain. –  David Carraher Nov 17 '13 at 18:29
@DavidCarraher No, my aim is to find if the pixels in one image are correlated. For my purposes though it is sufficient to look at the covariance, instead of the correlation. –  phidelio Nov 17 '13 at 19:33

This follows Michael's interpretation of the question and gives an $M \times N \times M \times N$ array in which the element with indices $g,h,i,j\,$ is the covariance, over the $P$ images, of the pixels in row $g$, column $h$ with the pixels in row $i$, column $j$.

With[{n = Last@Dimensions@tiffs},
Partition[Partition[#,n]& /@ Covariance[Flatten/@tiffs], n]]

EDIT - Flatten/@tiffs converts the $P \times M \times N$ array into a $P \times MN$ matrix in which each image is a single row; the pixel in row $i$, column $j$ of image $k$ is in row $k$, column $(i-1)N+j$ of the new matrix. Covariance gets an $MN \times MN$ matrix of covariances, which the Partitioning reorganizes into an $M \times N \times M \times N$ array, say cov.

The following code will give an $M \times N$ matrix sd containing the standard deviations of the pixels in each position and an $M \times N \times M \times N$ array corr of correlations, with corr[[g,h,i,j]] == cov[[g,h,i,j]]/(sd[[g,h]]*sd[[i,j]]).

{sd, corr} = With[{n = Last@Dimensions@tiffs},
{Partition[StandardDeviation@#, n],
Partition[Partition[#,n]& /@ Correlation@#, n]}&[Flatten/@tiffs]];

EDIT 2 - cov[[g,h,g,h]] == sd[[g,h]]^2, which is the variance of the pixels in row $g$, column $h$. If you want variances instead of standard deviations, just change StandardDeviation to Variance in the code and call the result var instead of sd. Then cov/sd^2 or cov/var will give you what you asked for, which is an array of regression coefficients, say b, in which b[[g,h,i,j]] is the coefficient in the ordinary least squares linear regression of the pixels in row $i$, column $j$ on the pixels in row $g$, column $h$.

-

I'm interpreting that you want to have a vector of length $P$, with the $i^\text{th}$ element representing the covariance of the $i^{th}$ matrix. You can try to use Map, whose documentation is given here:

http://reference.wolfram.com/mathematica/ref/Map.html

The code will simply look like Covariance/@A, where A is the stack of matrices you created. What it'll do is that it maps CovarianceA. In general, if you want to apply functions to lists, refer to this useful guide:

http://reference.wolfram.com/mathematica/guide/ApplyingFunctionsToLists.html

-
Yes, that is exactly what I wanted. Thank you, works like a charm. –  phidelio Nov 17 '13 at 19:35
I am sorry, I found that what I actually want is something different. –  phidelio Nov 17 '13 at 20:13
@phidelio AFAIK this is exactly what you're requesting in your edited version. Could you explain why you don't think so? –  belisarius Nov 17 '13 at 21:49
@belisarius I want a matrix with dimensions $M \times N$, where the $ij$-element is the covariance of vector $P_{sl}$ and $P_{ij}$. $s$ and $l$ run from 1 to $M$ and $N$ respectively. Then, I would have $M*N$ matrices. –  phidelio Nov 17 '13 at 22:38
To clarify: so for each point of the image, you want to measure how correlated it is with all of the other points in the image for the entire length of the video? –  DancingDarwin Nov 18 '13 at 1:44