<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238797</link>
    <title>MATLAB Central Newsreader - Check if two shifted matrices are equal</title>
    <description>Feed for thread: Check if two shifted matrices are equal</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 06 Nov 2008 19:59:02 -0500</pubDate>
      <title>Check if two shifted matrices are equal</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238797#609439</link>
      <author>Colin </author>
      <description>Hi all,&lt;br&gt;
&lt;br&gt;
I have two matrices that are 1 x 130943 and contain hex values.&lt;br&gt;
&lt;br&gt;
I want to be able to verify that they are in fact identical matrices but only one is shifted with respect to the other.&lt;br&gt;
&lt;br&gt;
A simple example could be:&lt;br&gt;
&lt;br&gt;
M1 =&lt;br&gt;
a&lt;br&gt;
b&lt;br&gt;
c&lt;br&gt;
d&lt;br&gt;
&lt;br&gt;
M2 =&lt;br&gt;
b&lt;br&gt;
c&lt;br&gt;
d&lt;br&gt;
a&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
Perhaps there is a faster/easier way to accomplish this?&lt;br&gt;
&lt;br&gt;
Thanks for any tips and insight,&lt;br&gt;
czhe</description>
    </item>
    <item>
      <pubDate>Thu, 06 Nov 2008 21:00:44 -0500</pubDate>
      <title>Re: Check if two shifted matrices are equal</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238797#609453</link>
      <author>Walter Roberson</author>
      <description>Colin wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; I have two matrices that are 1 x 130943 and contain hex values.&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;gt; I want to be able to verify that they are in fact identical matrices but only one is shifted&lt;br&gt;
&amp;gt; with respect to the other.&lt;br&gt;
&lt;br&gt;
Do you only need to know that they -are- identical (but perhaps shifted), or do you need to&lt;br&gt;
know the amount of the shift?&lt;br&gt;
&lt;br&gt;
And do you happen to have any constraint on how much shift there would be? For example,&lt;br&gt;
would it just happen to be the case that the shift would be by an even number of positions?&lt;br&gt;
&lt;br&gt;
Is there exactly 1 hex digit per array location?&lt;br&gt;
&lt;br&gt;
Would a fast &quot;Not the same&quot; or &quot;Might be the same&quot; be useful to you? e.g., are the&lt;br&gt;
arrays often different and being the same is the much less common case that you can&lt;br&gt;
afford to take longer to prove? For example, when two arrays are different, then&lt;br&gt;
their histograms would often be different, so if the histograms are different then&lt;br&gt;
you know [relatively quickly] that the arrays are not shifts of each other, and if&lt;br&gt;
the histograms are the same then you could run a more detailed test.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
.signature note: I am now avoiding replying to unclear or ambiguous postings.&lt;br&gt;
Please review questions before posting them. Be specific. Use examples of what you mean,&lt;br&gt;
of what you don't mean. Specify boundary conditions, and data classes and value&lt;br&gt;
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?</description>
    </item>
    <item>
      <pubDate>Thu, 06 Nov 2008 21:07:02 -0500</pubDate>
      <title>Re: Check if two shifted matrices are equal</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238797#609454</link>
      <author>Colin </author>
      <description>Walter Roberson &amp;lt;roberson@hushmail.com&amp;gt; wrote in message &amp;lt;vVIQk.3167$dy1.2828@newsfe01.iad&amp;gt;...&lt;br&gt;
&amp;gt; Colin wrote:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I have two matrices that are 1 x 130943 and contain hex values.&lt;br&gt;
&amp;gt;  &lt;br&gt;
&amp;gt; &amp;gt; I want to be able to verify that they are in fact identical matrices but only one is shifted&lt;br&gt;
&amp;gt; &amp;gt; with respect to the other.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Do you only need to know that they -are- identical (but perhaps shifted), or do you need to&lt;br&gt;
&amp;gt; know the amount of the shift?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; And do you happen to have any constraint on how much shift there would be? For example,&lt;br&gt;
&amp;gt; would it just happen to be the case that the shift would be by an even number of positions?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Is there exactly 1 hex digit per array location?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Would a fast &quot;Not the same&quot; or &quot;Might be the same&quot; be useful to you? e.g., are the&lt;br&gt;
&amp;gt; arrays often different and being the same is the much less common case that you can&lt;br&gt;
&amp;gt; afford to take longer to prove? For example, when two arrays are different, then&lt;br&gt;
&amp;gt; their histograms would often be different, so if the histograms are different then&lt;br&gt;
&amp;gt; you know [relatively quickly] that the arrays are not shifts of each other, and if&lt;br&gt;
&amp;gt; the histograms are the same then you could run a more detailed test.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; -- &lt;br&gt;
&amp;gt; .signature note: I am now avoiding replying to unclear or ambiguous postings.&lt;br&gt;
&amp;gt; Please review questions before posting them. Be specific. Use examples of what you mean,&lt;br&gt;
&amp;gt; of what you don't mean. Specify boundary conditions, and data classes and value&lt;br&gt;
&amp;gt; relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?&lt;br&gt;
&lt;br&gt;
Hi! Thanks for the quick reply.&lt;br&gt;
&lt;br&gt;
Your suggestions are very helpful. It would definitely be beneficial for me if I could tell right away if the matrices do not match. Could I do this without any manipulation of the matrices themselves?&lt;br&gt;
&lt;br&gt;
If I find that they are indeed matching, I still need to perform the shift so they match exactly, so I suppose I would need to know how much they are shifted by.</description>
    </item>
    <item>
      <pubDate>Thu, 06 Nov 2008 21:12:01 -0500</pubDate>
      <title>Re: Check if two shifted matrices are equal</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238797#609457</link>
      <author>Colin </author>
      <description>Walter Roberson &amp;lt;roberson@hushmail.com&amp;gt; wrote in message &amp;lt;vVIQk.3167$dy1.2828@newsfe01.iad&amp;gt;...&lt;br&gt;
&amp;gt; Colin wrote:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I have two matrices that are 1 x 130943 and contain hex values.&lt;br&gt;
&amp;gt;  &lt;br&gt;
&amp;gt; &amp;gt; I want to be able to verify that they are in fact identical matrices but only one is shifted&lt;br&gt;
&amp;gt; &amp;gt; with respect to the other.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Do you only need to know that they -are- identical (but perhaps shifted), or do you need to&lt;br&gt;
&amp;gt; know the amount of the shift?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; And do you happen to have any constraint on how much shift there would be? For example,&lt;br&gt;
&amp;gt; would it just happen to be the case that the shift would be by an even number of positions?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Is there exactly 1 hex digit per array location?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Would a fast &quot;Not the same&quot; or &quot;Might be the same&quot; be useful to you? e.g., are the&lt;br&gt;
&amp;gt; arrays often different and being the same is the much less common case that you can&lt;br&gt;
&amp;gt; afford to take longer to prove? For example, when two arrays are different, then&lt;br&gt;
&amp;gt; their histograms would often be different, so if the histograms are different then&lt;br&gt;
&amp;gt; you know [relatively quickly] that the arrays are not shifts of each other, and if&lt;br&gt;
&amp;gt; the histograms are the same then you could run a more detailed test.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; -- &lt;br&gt;
&amp;gt; .signature note: I am now avoiding replying to unclear or ambiguous postings.&lt;br&gt;
&amp;gt; Please review questions before posting them. Be specific. Use examples of what you mean,&lt;br&gt;
&amp;gt; of what you don't mean. Specify boundary conditions, and data classes and value&lt;br&gt;
&amp;gt; relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?&lt;br&gt;
&lt;br&gt;
Sorry and to address your other questions, the matrix could be shifted by any arbitrary integer number up to the size of the matrix minus 1.&lt;br&gt;
&lt;br&gt;
There is a hex value at exactly every index.</description>
    </item>
    <item>
      <pubDate>Thu, 06 Nov 2008 23:16:04 -0500</pubDate>
      <title>Re: Check if two shifted matrices are equal</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238797#609487</link>
      <author>Roger Stafford</author>
      <description>&quot;Colin &quot; &amp;lt;colinzhe@gmail.com&amp;gt; wrote in message &amp;lt;gevia6$2hk$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi all,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have two matrices that are 1 x 130943 and contain hex values.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I want to be able to verify that they are in fact identical matrices but only one is shifted with respect to the other.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A simple example could be:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; M1 =&lt;br&gt;
&amp;gt; a&lt;br&gt;
&amp;gt; b&lt;br&gt;
&amp;gt; c&lt;br&gt;
&amp;gt; d&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; M2 =&lt;br&gt;
&amp;gt; b&lt;br&gt;
&amp;gt; c&lt;br&gt;
&amp;gt; d&lt;br&gt;
&amp;gt; a&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Perhaps there is a faster/easier way to accomplish this?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks for any tips and insight,&lt;br&gt;
&amp;gt; czhe&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;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.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;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.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Fri, 07 Nov 2008 00:59:02 -0500</pubDate>
      <title>Re: Check if two shifted matrices are equal</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238797#609497</link>
      <author>Bruno Luong</author>
      <description>&quot;Colin &quot; &amp;lt;colinzhe@gmail.com&amp;gt; wrote in message &amp;lt;gevia6$2hk$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Perhaps there is a faster/easier way to accomplish this?&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
a=[7   0   3   0   1   8   7   3  10   0];&lt;br&gt;
b=[3   0   1   8   7   3  10   0   7   0];&lt;br&gt;
&lt;br&gt;
% Engine&lt;br&gt;
shift=findstr(b,[a a])-1&lt;br&gt;
&lt;br&gt;
% Check&lt;br&gt;
n = length(a);&lt;br&gt;
&lt;br&gt;
if isempty(shift)&lt;br&gt;
&amp;nbsp;&amp;nbsp;fprintf('cannot match after circular shift')&lt;br&gt;
else&lt;br&gt;
&amp;nbsp;&amp;nbsp;shift = shift(1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;shiftedidx = mod((1:n)+shift-1,length(a))+1;&lt;br&gt;
&amp;nbsp;&amp;nbsp;isequal(b, a(shiftedidx ))&lt;br&gt;
end&lt;br&gt;
&amp;nbsp;&lt;br&gt;
% Bruno</description>
    </item>
    <item>
      <pubDate>Fri, 07 Nov 2008 09:51:02 -0500</pubDate>
      <title>Re: Check if two shifted matrices are equal</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238797#609540</link>
      <author>Jos </author>
      <description>&quot;Colin &quot; &amp;lt;colinzhe@gmail.com&amp;gt; wrote in message &amp;lt;gevia6$2hk$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi all,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have two matrices that are 1 x 130943 and contain hex values.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I want to be able to verify that they are in fact identical matrices but only one is shifted with respect to the other.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A simple example could be:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; M1 =&lt;br&gt;
&amp;gt; a&lt;br&gt;
&amp;gt; b&lt;br&gt;
&amp;gt; c&lt;br&gt;
&amp;gt; d&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; M2 =&lt;br&gt;
&amp;gt; b&lt;br&gt;
&amp;gt; c&lt;br&gt;
&amp;gt; d&lt;br&gt;
&amp;gt; a&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Perhaps there is a faster/easier way to accomplish this?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks for any tips and insight,&lt;br&gt;
&amp;gt; czhe&lt;br&gt;
&lt;br&gt;
This is pretty quick:&lt;br&gt;
&lt;br&gt;
%== Start of Code ==&lt;br&gt;
% some data&lt;br&gt;
a = rand(100000,1) ;&lt;br&gt;
% create a shifted vector&lt;br&gt;
nc = ceil(numel(a)*rand)  &lt;br&gt;
b = circshift(a,nc) ; % shifted&lt;br&gt;
&lt;br&gt;
% try different types&lt;br&gt;
% b = rand(size(a)) ; % completely different&lt;br&gt;
% b([1 2]) = b([2 1]) ; % swap two elements&lt;br&gt;
% b = a ;  % no shift&lt;br&gt;
&lt;br&gt;
[i,i] = ismember(a,b) ;&lt;br&gt;
di = abs(diff(i)) ;&lt;br&gt;
ShiftN = - find(di&amp;gt;1) ; % minus sign!&lt;br&gt;
&lt;br&gt;
AreTheSame = numel(ShiftN)&amp;lt;2 &lt;br&gt;
if AreTheSame,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ShiftN = max([ShiftN ; numel(a) + ShiftN ; 0])&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b2 = circshift(a,ShiftN) ;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;isequal(b2,b) &lt;br&gt;
end&lt;br&gt;
%== End of Code ==&lt;br&gt;
&lt;br&gt;
hth&lt;br&gt;
Jos</description>
    </item>
    <item>
      <pubDate>Fri, 07 Nov 2008 15:24:02 -0500</pubDate>
      <title>Re: Check if two shifted matrices are equal</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238797#609601</link>
      <author>Colin </author>
      <description>Thank you all very much! This helps a lot.</description>
    </item>
  </channel>
</rss>

