Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Check if two shifted matrices are equal
Date: Fri, 7 Nov 2008 09:51:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 56
Message-ID: <gf1326$39p$1@fred.mathworks.com>
References: <gevia6$2hk$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1226051462 3385 172.30.248.37 (7 Nov 2008 09:51:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 7 Nov 2008 09:51:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 870065
Xref: news.mathworks.com comp.soft-sys.matlab:499464


"Colin " <colinzhe@gmail.com> wrote in message <gevia6$2hk$1@fred.mathworks.com>...
> Hi all,
> 
> I have two matrices that are 1 x 130943 and contain hex values.
> 
> I want to be able to verify that they are in fact identical matrices but only one is shifted with respect to the other.
> 
> A simple example could be:
> 
> M1 =
> a
> b
> c
> d
> 
> M2 =
> b
> c
> d
> a
> 
> My first thought was to use the 'circshift' function on one matrix and run it through a loop until the two matrices match exactly. Unfortunately, this takes a very long time.
> 
> Perhaps there is a faster/easier way to accomplish this?
> 
> Thanks for any tips and insight,
> czhe

This is pretty quick:

%== Start of Code ==
% some data
a = rand(100000,1) ;
% create a shifted vector
nc = ceil(numel(a)*rand)  
b = circshift(a,nc) ; % shifted

% try different types
% b = rand(size(a)) ; % completely different
% b([1 2]) = b([2 1]) ; % swap two elements
% b = a ;  % no shift

[i,i] = ismember(a,b) ;
di = abs(diff(i)) ;
ShiftN = - find(di>1) ; % minus sign!

AreTheSame = numel(ShiftN)<2 
if AreTheSame,
    ShiftN = max([ShiftN ; numel(a) + ShiftN ; 0])
    b2 = circshift(a,ShiftN) ;
    isequal(b2,b) 
end
%== End of Code ==

hth
Jos