I am trying to derive the covariance results for a $n \times 4$ matrix where $n$ is an arbitrary number.

I am trying to derive the covariance for each row cumulatively. For example, I am trying to derive the covariance for this matrix from row 1 to row 2, then from row 1 to 3, row 1 to 4, row 1 to 5, and so on such as:

result[i_] := Covariance[{{rc1[[1 ;; i]]}, {rc2[[1 ;; i]]}, 
                          {rc2[[1 ;; i]]}, {rc2[[1 ;; i]]}}]

where rc1, rc2, rc3, rc4 are arbitrary list of numbers with only one column.

I tried this code and it does not seem to work as well but I am trying to do the work something like this.

I would appreciate if I could get help on this. Thank you.

  • $\begingroup$ Is this what you are looking for? n = 5; rc = RandomReal[{0, 1}, {n, 4}]; Table[ Covariance[rc[[1 ;; i]]], {i, 2, n}] $\endgroup$ – Nasser Mar 14 '15 at 11:19
  • $\begingroup$ Thank you very much for your reply. Actually, I am trying to produce covariance results for a matrix which has four columns with an arbitrary number of rows which can be any. Therefore, the 4 columns should contain all different rows.. $\endgroup$ – Eric Mar 14 '15 at 11:22
  • $\begingroup$ isn't that what I wrote? n is number of rows. $\endgroup$ – Nasser Mar 14 '15 at 11:23
  • $\begingroup$ Thank you. Yes, you are right. However, when I am trying to construct one matrix with four columns using the list from rc1, rc2, rc3, rc4, then is it correct to construct like {rc1,rc2,rc3,rc4}? I think combining like this will solve the problem as your example shows. $\endgroup$ – Eric Mar 14 '15 at 12:04
  • $\begingroup$ For example, when I derive the covariance results using four lists from row 1 to row 3 such as Covariance[{rc1[[1 ;; 3]], rc2[[1 ;; 3]], rc3[[1 ;; 3]], rc4[[1 ;; 3]]}] the result gives 3 rows and 3 columns where I am expecting just 3 rows. But it is very strange that when I execute the command {rc1[[1 ;; 3]], rc2[[1 ;; 3]], rc3[[1 ;; 3]], rc4[[1 ;; 3]]} it gives out 3 rows and 4 columns as expected. May I know how I can resolve this? $\endgroup$ – Eric Mar 14 '15 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.