Determining if any duplicate rows in two matrices in MatLab -
introduction problem:
i'm modelling system have matrix x=([0,0,0];[0,1,0],...) each row represent point in 3d-space. choose random row, r, , take following rows , rotate around point represented r, , make new matrix these rows, x_rot. want check whether of rows x_rot equal 2 of rows of x (i.e. 2 vertices on top of each other), , if case refuse rotation , try again.
actual question:
until have used following code:
x_sim=[x;x_rot]; if numel(unique(x_sim,'rows'))==numel(x_sim); x(r+1:n+1,:,:)=x_rot; end
which works, takes on 50% of running time , considering if in here knew more efficient way it, since don't need information unique
.
p.s. if matters typically have between 100 , 1000 rows in x
.
best regards, morten
additional: x
-matrix contains n+1 rows , have 12 different rotational operations can apply sub-matrix x_rot:
step=ceil(rand()*n); r=ceil(rand()*12); x_rot=x(step+1:n+1,:); x_rot=bsxfun(@minus,x_rot,x(step,:)); x_rot=x_rot*rot(:,:,:,r); x_rot=bsxfun(@plus,x_rot,x(step,:));
two possible approaches (i don't know if faster using unique
):
use
pdist2
:d = pdist2(x, x_rot, 'hamming'); %// 0 if rows equal, 1 if different. %// distance function do, try available , choose fastest result = any(d(:)==0);
use
bsxfun
:d = squeeze(any(bsxfun(@ne, x, permute(x_rot, [3 2 1])), 2)); result = any(d(:)==0);
result
1
if there row of x
equal row of x_rot
, , 0
otherwise.
Comments
Post a Comment