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

  • $\begingroup$ You have been an user for 9 months and still don't know how to properly format your question ?! I don't believe this .. $\endgroup$
    – Sektor
    Nov 17, 2013 at 16:12
  • $\begingroup$ @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? $\endgroup$ Nov 17, 2013 at 16:15
  • $\begingroup$ My words have been harsh, truth, but there's a help centre which outlines the good formatting practices. $\endgroup$
    – Sektor
    Nov 17, 2013 at 16:22
  • $\begingroup$ What is your aim? To find out how much one image matches another image? Kindly explain. $\endgroup$
    – DavidC
    Nov 17, 2013 at 18:29
  • $\begingroup$ @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. $\endgroup$ Nov 17, 2013 at 19:33

2 Answers 2


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:


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:


  • $\begingroup$ Yes, that is exactly what I wanted. Thank you, works like a charm. $\endgroup$ Nov 17, 2013 at 19:35
  • $\begingroup$ I am sorry, I found that what I actually want is something different. $\endgroup$ Nov 17, 2013 at 20:13
  • $\begingroup$ @phidelio AFAIK this is exactly what you're requesting in your edited version. Could you explain why you don't think so? $\endgroup$ Nov 17, 2013 at 21:49
  • $\begingroup$ @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. $\endgroup$ Nov 17, 2013 at 22:38
  • $\begingroup$ 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? $\endgroup$ Nov 18, 2013 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.