0

I want to calculate covariance of 400 50 X 50 matrices using loop so when I tried..

for i=t-9:t+59
    for j=r-9:r+59
        a(:,:,p)=vidFrames2(i:(i+49),j:(j+49));
        b(:,:,p)=cov3d(double(a));
        p=p+1;     
    end
end

this code I got error "Assignment has more non-singleton rhs dimensions than non-singleton subscripts" but when i want to remove this error by squeeze function in this code,

 b(p,:)=cov3d(squeeze(double(a)));

I got "Subscripted assignment dimension mismatch." how can I calculate covariance of 3d matrix....the cov3d function is-

function xy = cov3d(x)
[m,n,r] = size(x);
if m==1
x=zeros(n,n,r,class(x));
else
xc=bsxfun(@minus,x,sum(x,1)/m);
for i=1:r
    xci=xc(:,:,i);
    xy(:,:,i)=xci'*xci;
end
xy=xy/(m-1);

end actually i wanted 400 number of covariance matrices stored in a variable.after this change i only get one covariance matrix.actually i am working on a frame where i am storing the initial coordinate values in i and j of a 50 X 50 matrix. then i want to get the pixel values of the 10 X 10 neighbourhood in the form of 50 X 50 matix. that means if i entered the initial coordinate as (100,100), then i want to get pixel values stored within region (91,91)(91,141)(141,91)(141,141).then store the covariance value of this matix. then again it will find the pixel values of the region (92,91),(142,91)(92,141),(142,141)and so on and covers the whole area..so if i cover the whole area then i think i will get total 400 matrices.and i want to store the covariance value of all the regions,.. today i changed the upper limit of the for loops to i=t-9:t+60 and j=r-9:r+60 but i did not get 400 covariance matrices

S Singha
  • 41
  • 1
  • 6
  • I see that these 50 by 50 matrices appear to be frames from a video. Are you aiming to find a 2x2 covariance matrix for each frame in the video based on the distribution of pixel intensities? – eigenchris Feb 03 '15 at 16:00
  • @eigenchris - The OP pulled the `cov3d` code from here: http://stackoverflow.com/questions/17435510/calculating-the-covariance-of-a-1000-5x5-matrices-in-matlab – rayryeng Feb 03 '15 at 16:05

1 Answers1

2

A few comments about your code:

  1. cov3d is designed to return a 3D matrix, where the input is also a 3D matrix that calculates the covariance for each slice in this 3D matrix. From your code, the outer for loops (indices i and j) go from t-9 to t+59 in steps of 1 for each variable. As such, the pair of loops will execute ((t+59) - (t-9) + 1)^2 = 69^2 = 4761 times. I'm assuming that at each iteration, you are extracting a 2D matrix located at a which is why you initially said 400 times. You basically want to extract 400 matrices, but you are actually extracting 4761 matrices. Make sure a is properly declared to account for this many elements.

  2. a(:,:,p)=vidFrames2(i:(i+49),j:(j+49)); accesses one slice, yet b(:,:,p)=cov3d(double(a)); is accessing the entire 3D matrix of a. Therefore, you will get a 3D matrix as the output of cov3d, yet you are trying to assign this to a single slice in b. What you should probably do is create your a matrix first, then make one call to cov3d when you're done.


Therefore, try changing your code to this:

%// Create `a` matrix first
for i=t-9:t+59
    for j=r-9:r+59
        a(:,:,p)=vidFrames2(i:(i+49),j:(j+49));
        p=p+1;     
    end
end

%// Now call cov3d
b = cov3d(double(a));

Hopefully this will work the way you intended your code to originally work.

Community
  • 1
  • 1
rayryeng
  • 102,964
  • 22
  • 184
  • 193