Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Check if two shifted matrices are equal
Date: Thu, 6 Nov 2008 23:16:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 34
Message-ID: <gevtrk$3vk$1@fred.mathworks.com>
References: <gevia6$2hk$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1226013364 4084 172.30.248.38 (6 Nov 2008 23:16:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 6 Nov 2008 23:16:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:499410


"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

  You might consider two nested while-loops in which the outer one moves through possible shift amounts and the inner one does a match one at a time with individual elements shifted by that amount until either a mismatch is found or the entire shifted vectors are found to match.  The outer loop would exit either when a total match is found or when all possible shift amounts have been tried without success.

  This would tend to minimize the total number of comparisons that are necessary, and for the large size you are considering that might be important.  That is, doing a 'circshift' followed by a vector comparison might well take more time on the average, even though vectorized, than comparing elements one at a time in a while-loop until the first mismatch occurs.

Roger Stafford