Re: circular shifting a vector/matrix - Problem
- From: "karvala" <karvala21@xxxxxxxxxxx>
- Date: Sun, 30 Apr 2006 15:52:00 +0100
Unfortunately circshift is a handy function, but terribly slow. I've just
knocked up this fast and dirty benchmark to give you an idea of how to do
the same thing, in a very straigthforward way and still in a loop if that's
what you need in your algorithm, but without using circshift. Comment out
one or other line inside the loops to measure the performance of that
particular method. You'll see that the simple single matrix duplication
method is faster by several orders of magnitude:-
clear all; clc; close all
% Matrices
a = rand(5,20); % original matrix
c = size(a,2); % no. columns in original matrix
tic
b = repmat(a,1,2); % duplicated and concatenated (*comment out for
circshift method benchmark*)
for k = 1:1000 % loop simply to make longer for benchmark purposes
for j = 1:c % loop over column shift positions
d = b(:,j:(j+c-1)); % replication method (*comment out for
circshift method benchmark*)
%d = circshift(a,[0 -j]); % circshift method (*comment out for
replication method benchmark*)
end
end
toc
"jkvdpoel" <jkvdpoel@xxxxxxxxx> wrote in message
news:1146363422.047346.98370@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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
.
- References:
- circular shifting a vector/matrix - Problem
- From: jkvdpoel
- circular shifting a vector/matrix - Problem
- Prev by Date: Re: to input zeros randomly into matrix
- Next by Date: Re: RS(1901,1855)
- Previous by thread: Re: circular shifting a vector/matrix - Problem
- Next by thread: Calculating empirical joint distribution
- Index(es):
Relevant Pages
|