Re: circular shifting a vector/matrix - Problem
- From: ellieandrogerxyzzy@xxxxxxxxxxxxxxxxxxxxxx (Roger Stafford)
- Date: Sun, 30 Apr 2006 07:00:39 GMT
In article <1146363422.047346.98370@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"jkvdpoel" <jkvdpoel@xxxxxxxxx> wrote:
Hello,----------------------
I have a problem with circular shifting a vector/matrix that is keeping
my mind absolutely crazy.
I know and use the "circshift" function (many thanks to the one that
designed this function, as it helps me to solve almost all of my
problems ;-).
I have two matrices, each one with equal dimensions M-by-N, and I am
using "corr2" to calculate the two-dimensional correlation coefficient
between them. The second matrix is "almost similar" to the first one,
but its columns are rotated in relation to the columns of the first
one.
So, I am using "corr2" as a way to compute the similarity between these
two matrices. As one is shifted to the other, I compute "corr2" between
them, rotate by one column one of the matrices, compute "corr2" between
them, then rotate again, compute "corr2", and so on...
Each time I rotate one of the matrices, I keep the correlation
coefficient. At the end, when all columns are shifted I end up with a
vector of correlation coefficients. Then I compute
max(vector_coef_corr) to see where the two matrices are most similar to
each other.
My problem is that, as my matrices have a large number of columns, the
circular shifting is a time consuming procedure (of course I know that
"corr2" is also a time consuming procedure, but I _have_ to use this
function - as it is part of the idea of my algorithm). How can I speed
the circular shifting process? I am using a for loop (argh!!!!!)
nowadays, because I couldn't thing in any other solution (-:. Is there
a way to vectorize this thing?
Here you have a piece of my code:
corrCoef = zeros(1, numColMatrix01);
for shiftSize = 1:numColMatrix01,
corrCoef(shiftSize) = corr2(Matrix01, Matrix02);
Matrix01 = circshift(Matrix01, [0 shiftSize]);
end;
corrCoef = max(corrCoef);
Many thanks in advance,
JKvdPoel
Whether you can use the following code or not depends on whether size
m*n by n matrices can be successfully fitted into your memory. That is
the size of the B0(I) matrix below. Let A = Matrix01 and B = Matrix02.
A0 = A - mean(mean(A));
B0 = B - mean(mean(B));
[m,n] = size(A);
I = rem(repmat([0:m*n-1]',1,n)+repmat([m*n:-m:m],m*n,1),m*n)+1;
[mx,k] = max(A0(:)'*B0(I));
The index k tells which circular shift of B best correlates with A, and mx
is the corresponding covariance. (You have to divide by the two standard
deviations to get the correlation if you actually need it.)
There is no point in repeatedly computing the denominators of 'corr2'
for each of the circular shifts because they are the same for all shifted
versions. Creation of I above gets around having to go through the
for-loop repeatedly by generating indices for all circular shifts
simultaneously. The m*n by n matrix B0(I) contains all circular shifts of
B0. The multiplication in the last line carries out the numerator
calculation (the covariance) for all n circular shifts.
I can't guarantee this is faster than your for-loop method, however,
since my version of matlab is quite ancient. That is your job to find
out.
I have only tested this in a cursory way, since I do not possess either
'circshift' or 'corr2', so you would be well-advised to repeatedly compare
the results of the above with your own for-loop to check out its accuracy.
(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford
.
- References:
- circular shifting a vector/matrix - Problem
- From: jkvdpoel
- circular shifting a vector/matrix - Problem
- Prev by Date: mbuild error
- Next by Date: Koshkitko
- Previous by thread: circular shifting a vector/matrix - Problem
- Next by thread: Re: circular shifting a vector/matrix - Problem
- Index(es):
Relevant Pages
|