correlation of an array

i've an array in which i've stored different matrices. i want to calculate the correlation between the matrices stored in an array
like:
A=[ 12 23 12 34 21 32 43 65 65 76 1 23 43 21 54 98 ]
21 43 2 13 76 87 34 22 89 67 45 88 55 77 54 65
32 45 56 78 89 87 84 54 22 33 43 54 78 98 97 65
67 54 33 44 88 77 66 54 21 15 16 48 90 80 21 66
like if i've array=[A;B;C;D]
i want the correlation between A and B
correlation between A and C
correlation between A and D
correlation between B and C
correlation between B and D
correlation between C and D
help me in doing so

2 comentarios

Tobias
Tobias el 8 de Abr. de 2013
Isn't that basicly the xcorr function? I have not worked with that before, but did you check Cross-correlation?
Raman
Raman el 8 de Abr. de 2013
xcorr2 is fine but how make a loop on it so that it can calculate all the above combination which mentioned above....
for k1=1:length(A)
A1=A(k1)
for k2=k1:length(A)
A2=A(k2)
correlation=xcorr2(k1,k2);
fprintf('%d',correlation);
end
end
the above code calculates the correlation in only one matrix i'e correlation between one pixel and the second pixel in the same matrix... i want to calculate correlation between the different matrix as the combination i've given above...

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 8 de Abr. de 2013

1 voto

Extract the 4 submatrices and correlate all the permutations.
A = fullMatrix(:, 1:4);
B = fullMatrix(:, 5:8);
C = fullMatrix(:, 9:12);
D = fullMatrix(:, 13:16);
% Now correlate:
AB = xcorr2(A, B);
AC = xcorr2(A, C);
AD = xcorr2(A, D);
BC = xcorr2(B, C);
BD = xcorr2(B, D);
CD = xcorr2(C, D);
There is no need for complicated loops when you have this few matrices, and just a few simple lines of code will do it for you.

8 comentarios

Raman
Raman el 8 de Abr. de 2013
this is ok sir but what if i've a loop and each time m storing a different values in matrix then the above code works how?
can u tell me how can i do so?
like i've an array
B1=[12 34 56] here 12 34 and 56 are block number of an image
L=length(B1);
for b=1:L
blockNumber1 = B1(b);
fprintf('\n block %d\n',B1(b));
blockrows =16; blockcols = 16;
[blockcol, blockrow] = ind2sub( [blockcols, blockrows], blockNumber1);
colstart = (blockcol-1) * blockSizeC + 1;
rowstart = (blockrow - 1) * blockSizeR + 1;
oneBlock1=grayImage(rowstart : rowstart + blockSizeR - 1, colstart : colstart + blockSizeC - 1);
%fprintf('\n oneblock------------------------1\n');
disp(oneBlock1);
end
here i want to store each matrix in array and want to find the correlation between these matrices as i told u earlier...
the above 3 blocks(block 12, block 34 and block 56) has its corresponding intensity matrix i want to calculate the correlation between these matrices i.e
correlation between block 12 matrix and block 34 matrix
correlation between block 12 matrix and block 56 matrix
correlation between block 34 matrix and block 56 matrix
can u tell me plz sir how it can be done?
Image Analyst
Image Analyst el 8 de Abr. de 2013
Why do you want a for loop? It's just more complicated that way.
Raman
Raman el 9 de Abr. de 2013
this is in my project...m just giving u a hint that my codes is something like this...here i've got three different matrices corresponding to oneBlock1.. now u tell me how to do this in order to find the correlation between these matrices...
Image Analyst
Image Analyst el 9 de Abr. de 2013
I think you're on your way. You just need to get rowstart and colstart correct for each block, and have two loops one over 1 to L and the other from there to L
for k1 = 1 : L
for k2 = k1+1 : L
% Get blocks k1 and k2 and call xcorr2()
end
end
angel
angel el 9 de Abr. de 2013
one more thing sir the code i've given u, in that oneBlock1 is giving me a matrix right?
i want to store it in a array can u help me in storing this as i m confused...
for b=1:L
blockNumber1 = B1(b);
fprintf('\n block %d\n',B1(b));
blockrows =16; blockcols = 16;
[blockcol, blockrow] = ind2sub( [blockcols, blockrows], blockNumber1);
colstart = (blockcol-1) * blockSizeC + 1;
rowstart = (blockrow - 1) * blockSizeR + 1;
oneBlock1=grayImage(rowstart : rowstart + blockSizeR - 1, colstart : colstart + blockSizeC - 1);
the following line of code i wrote for storing matrices in array is it correct or not?
[oneBlock2]=oneBlock1;
if not correct it for me sir....
Image Analyst
Image Analyst el 9 de Abr. de 2013
oneBlock1 will be a matrix of the same type as grayImage. You don't need brackets around oneBlock2.
Raman
Raman el 9 de Abr. de 2013
Editada: Raman el 9 de Abr. de 2013
ok then what should i do to store oneBlock in an array so that i can calculate the correlation between the matrices...
12 34 56 78
56 78 89 21
66 43 22 32
23 43 76 89
the code i wrote caculates
correlation between 12 and 56
correlation between 12 and 66
correlation between 12 and 23
correlation between 56 and 66
correlation between 56 and 23
correlation between 66 and 23
this i dont want...
i want it to calculate correlation like this if
12 37 56 78 34 99 11 21
56 78 89 21 10 40 89 90
66 43 22 32 45 43 22 55
23 43 76 89 56 44 23 22
i want to calculate
correlation between 12 and 34
correlation between 37 and 99
correlation between 56 and 11
correlation between 78 and 21
correlation between 56 and 10
correlation between 78 and 40
and so on......
i.e correlation between first element of matrice 1 and matrix 2, then second element of matrix 1 and matrix 2 and so on..... how can i acheive this sir?
help me to do so...
Image Analyst
Image Analyst el 9 de Abr. de 2013
I don't understand - those look like pixel values, not ID numbers of blocks. I thought you just had 16 blocks and each block was a 4 by 4 array. You wouldn't compute correlation of single pixel values - you do it with 2D arrays. But frankly I don't know what you're after since you didn't give us the whole picture, the larger context, so I don't know if your approach is even correct to do what you think you want to do, or not.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 8 de Abr. de 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by