<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238541</link>
    <title>MATLAB Central Newsreader - compare two vectors</title>
    <description>Feed for thread: compare two vectors</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>Sun, 02 Nov 2008 00:45:03 -0400</pubDate>
      <title>compare two vectors</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238541#608546</link>
      <author>ann au</author>
      <description>Hi,&lt;br&gt;
&lt;br&gt;
I have two vectors:&lt;br&gt;
&lt;br&gt;
a=[1 1 1 1 1 2 2 3 4 5 6 7]'&lt;br&gt;
b = [1 1 2 2 5 6]'&lt;br&gt;
&lt;br&gt;
I want to get the following result:&lt;br&gt;
r = [1 1 1 3 4 7]'&lt;br&gt;
&lt;br&gt;
What code can I use?&lt;br&gt;
Thanks a lot!&lt;br&gt;
&lt;br&gt;
Ann</description>
    </item>
    <item>
      <pubDate>Sun, 02 Nov 2008 02:38:02 -0500</pubDate>
      <title>Re: compare two vectors</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238541#608556</link>
      <author>Image Analyst</author>
      <description>&quot;ann au&quot; &amp;lt;fannylyy@hkusua.hku.hk&amp;gt; wrote in message &amp;lt;geit6f$8ev$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have two vectors:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a=[1 1 1 1 1 2 2 3 4 5 6 7]'&lt;br&gt;
&amp;gt; b = [1 1 2 2 5 6]'&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I want to get the following result:&lt;br&gt;
&amp;gt; r = [1 1 1 3 4 7]'&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; What code can I use?&lt;br&gt;
&amp;gt; Thanks a lot!&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Ann&lt;br&gt;
-----------------------------------------------------&lt;br&gt;
Not exactly sure what you're after, or what algorithm you're using, but this will work   ;-)&lt;br&gt;
&lt;br&gt;
a=[1 1 1 1 1 2 2 3 4 5 6 7]'&lt;br&gt;
b = [1 1 2 2 5 6]'&lt;br&gt;
r = CompareVectors(a, b);&lt;br&gt;
&lt;br&gt;
function returnArray=CompareVectors(a, b)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;returnArray = [1 1 1 3 4 7];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return;</description>
    </item>
    <item>
      <pubDate>Sun, 02 Nov 2008 04:16:02 -0500</pubDate>
      <title>Re: compare two vectors</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238541#608559</link>
      <author>Husam Aldahiyat</author>
      <description>I think it's a riddle.</description>
    </item>
    <item>
      <pubDate>Sun, 02 Nov 2008 05:48:02 -0500</pubDate>
      <title>Re: compare two vectors</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238541#608562</link>
      <author>Seth </author>
      <description>Maybe you are looking for something like this that returns the dissimilar elements?&lt;br&gt;
&lt;br&gt;
a=[1 1 1 1 1 2 2 3 4 5 6 7]';&lt;br&gt;
b = [1 1 2 2 5 6]';&lt;br&gt;
r = zeros(1,length(a)-length(b));&lt;br&gt;
r_c = 1;&lt;br&gt;
&lt;br&gt;
for i = 1:max(a)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;k = (sum(a==i) - sum(b==i));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if k &amp;gt; 0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for j = 1:k&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r(r_c) = i;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r_c = r_c + 1;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
Seth&lt;br&gt;
&lt;br&gt;
&quot;Image Analyst&quot; &amp;lt;imageanalyst@mailinator.com&amp;gt; wrote in message &amp;lt;gej3qa$609$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;ann au&quot; &amp;lt;fannylyy@hkusua.hku.hk&amp;gt; wrote in message &amp;lt;geit6f$8ev$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Hi,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I have two vectors:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; a=[1 1 1 1 1 2 2 3 4 5 6 7]'&lt;br&gt;
&amp;gt; &amp;gt; b = [1 1 2 2 5 6]'&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I want to get the following result:&lt;br&gt;
&amp;gt; &amp;gt; r = [1 1 1 3 4 7]'&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; What code can I use?&lt;br&gt;
&amp;gt; &amp;gt; Thanks a lot!&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Ann&lt;br&gt;
&amp;gt; -----------------------------------------------------&lt;br&gt;
&amp;gt; Not exactly sure what you're after, or what algorithm you're using, but this will work   ;-)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a=[1 1 1 1 1 2 2 3 4 5 6 7]'&lt;br&gt;
&amp;gt; b = [1 1 2 2 5 6]'&lt;br&gt;
&amp;gt; r = CompareVectors(a, b);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; function returnArray=CompareVectors(a, b)&lt;br&gt;
&amp;gt;     returnArray = [1 1 1 3 4 7];&lt;br&gt;
&amp;gt;     return;</description>
    </item>
    <item>
      <pubDate>Sun, 02 Nov 2008 06:04:48 -0500</pubDate>
      <title>Re: compare two vectors</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238541#608564</link>
      <author>Walter Roberson</author>
      <description>Husam Aldahiyat wrote:&lt;br&gt;
&amp;gt; I think it's a riddle.&lt;br&gt;
&lt;br&gt;
No, it's just the multi-set difference.&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>Sun, 02 Nov 2008 08:14:02 -0500</pubDate>
      <title>Re: compare two vectors</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238541#608568</link>
      <author>Roger Stafford</author>
      <description>&quot;ann au&quot; &amp;lt;fannylyy@hkusua.hku.hk&amp;gt; wrote in message &amp;lt;geit6f$8ev$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have two vectors:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a=[1 1 1 1 1 2 2 3 4 5 6 7]'&lt;br&gt;
&amp;gt; b = [1 1 2 2 5 6]'&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I want to get the following result:&lt;br&gt;
&amp;gt; r = [1 1 1 3 4 7]'&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; What code can I use?&lt;br&gt;
&amp;gt; Thanks a lot!&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Ann&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;I assume that for each element present in 'a' in greater quantity than in 'b' you wish 'r' to contain as many copies of that element as this difference.  That is, in your example 'r' has three 1's presumably because 'a' contains three more 1's than does 'b'.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;If that is what you intend, try this:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;e = union(a,b); % List all unique elements of combined a and b&lt;br&gt;
&amp;nbsp;n = max(histc(a,e)-histc(b,e),0); % How many of each e element&lt;br&gt;
&amp;nbsp;p = accumarray(cumsum(n)+1,1); % Proceed to generate r&lt;br&gt;
&amp;nbsp;p(1) = p(1)+1;&lt;br&gt;
&amp;nbsp;r = e(cumsum(p(1:end-1)));&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;The vector 'n' above contains the desired number of elements for 'r' of each of the corresponding elements of 'e'.  The remaining three lines are devoted to generating just such a vector 'r'.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Sun, 02 Nov 2008 12:43:02 -0500</pubDate>
      <title>Re: compare two vectors</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238541#608581</link>
      <author>Bruno Luong</author>
      <description>&quot;Roger Stafford&quot; &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in message &amp;lt;gejnga$3p9$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt;  The remaining three lines are devoted to generating just such a vector 'r'.&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
These &quot;remaining&quot; three lines are quite clever.&lt;br&gt;
&lt;br&gt;
I would suggest a more naive alternative for these three lines:&lt;br&gt;
&lt;br&gt;
r=arrayfun(@(ek,nk) repmat(ek,nk,1),e,n,...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'UniformOutput',false)&lt;br&gt;
r = cat(1,r{:})&lt;br&gt;
&lt;br&gt;
And here is another approach:&lt;br&gt;
&lt;br&gt;
e=union(a,b);&lt;br&gt;
[trast ia]=ismember(a,e);&lt;br&gt;
[trast ib]=ismember(b,e);&lt;br&gt;
count = @(i) {1:length(i)};&lt;br&gt;
ca = accumarray(ia(:,1), (1:length(a))', [], count);&lt;br&gt;
cb = accumarray(ib(:,1), (1:length(b))', [], count);&lt;br&gt;
ia(:,2)=cat(2,ca{:});&lt;br&gt;
ib(:,2)=cat(2,cb{:});&lt;br&gt;
&lt;br&gt;
r = a(~ismember(ia,ib,'rows'))&lt;br&gt;
&lt;br&gt;
% Bruno</description>
    </item>
  </channel>
</rss>

