<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Vectorized matrix look-up in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorized-matrix-look-up/m-p/385295#M3676</link>
    <description>&lt;P&gt;Thank you for you answer, Rick.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to extend and test your solution for a larger number of rows and columns for matrix 'a' and I ran into a problem. For 100 rows nad 5 columns, I receive this error:&amp;nbsp;&lt;STRONG&gt;ERROR: (execution) Matrices do not conform to the operation.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Please see below for code that generates some random data - I'm still not fully familiar with&amp;nbsp;ELEMENT function, but I can't find anything wrong with the&amp;nbsp;code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
	
	rows = 100;
	cols = 5; 

	min = 100;
	max = 1000;
	call randseed(555);

	a = j(rows,cols,.); 
	call randgen(a, "Uniform"); /*  ~ U(0,1) */
	a = min + floor( (1+Max-Min)*a );
	
	x1 = min:max;
	x1 = shape(x1,max-min+1,1);
	x2 = j(max-min+1,1,.);
	call randgen(x2, "Uniform"); /*  ~ U(0,1) */
	x2 = 10 + floor( (1+50-10)*x2 );

	b = x1||x2;


	/*vectorization*/
	c = j(nrow(a),ncol(a),.);

	do i = 1 to nrow(a);
		idx = loc( element(b[,1], a[i,]) ); /* location of elements in b[,1] that match a[i,] */
		c[i,] = T( b[idx,2] );     /* copy elements from b[,2] */
	end;

quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 03 Aug 2017 13:38:05 GMT</pubDate>
    <dc:creator>MDaniel</dc:creator>
    <dc:date>2017-08-03T13:38:05Z</dc:date>
    <item>
      <title>Vectorized matrix look-up</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorized-matrix-look-up/m-p/385269#M3674</link>
      <description>&lt;P&gt;I need to look-up the values from matrix 'a' in the first column of matrix 'b' and return the values in the &lt;EM&gt;second&lt;/EM&gt; column of matrix 'b' to a new matrix named 'c'. The rows in matrix 'b' are a key-value pairs, basically.&lt;/P&gt;&lt;P&gt;This code achieves that, but I would like to know whether there is a vectorized aproach to this instead of a do loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
	a = {100 202 303,
		107 200 303,
		101 205 302,
		102 200 300};

	b = {100 1,
		101 8,
		102 1,
		104 6,
		107 4,
		108 9,
		200 4,
		202 6,
		205 2,
		300 10,
		301 5,
		302 2,
		303 8,
		304 10};
	
	/*the Do LOOP way*/
	c = j(4,3,.);

	do i = 1 to 4;
		do j = 1 to 3;
			idx = loc(b[,1] = a[i,j]);
			c[i,j] = b[idx,2];
		end;
	end;

	print c;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The expected output in matrix 'c':&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;c 
1 6 8 
4 4 8 
8 2 2 
1 4 10 &lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2017 12:43:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorized-matrix-look-up/m-p/385269#M3674</guid>
      <dc:creator>MDaniel</dc:creator>
      <dc:date>2017-08-03T12:43:08Z</dc:date>
    </item>
    <item>
      <title>Re: Vectorized matrix look-up</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorized-matrix-look-up/m-p/385274#M3675</link>
      <description>&lt;P&gt;Yes. You want to use the ELEMENT function to find the vector of locations in b[,1] that match the elements in a[i,] for each row of a.&lt;/P&gt;
&lt;P&gt;You can read about the LOC-ELEMENT technique in the article &lt;A href="http://blogs.sas.com/content/iml/2015/05/11/loc-element-trick.html" target="_self"&gt;"Finding observations that satisfy multiple conditions: The LOC-ELEMENT technique"&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;c = j(nrow(a),ncol(a),.);
do i = 1 to nrow(a);
   idx = loc( element(b[,1], a[i,]) ); /* location of elements in b[,1] that match a[i,] */
   c[i,] = T( b[idx,2] );     /* copy elements from b[,2] */
end;
print c;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Aug 2017 12:53:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorized-matrix-look-up/m-p/385274#M3675</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-08-03T12:53:36Z</dc:date>
    </item>
    <item>
      <title>Re: Vectorized matrix look-up</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorized-matrix-look-up/m-p/385295#M3676</link>
      <description>&lt;P&gt;Thank you for you answer, Rick.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to extend and test your solution for a larger number of rows and columns for matrix 'a' and I ran into a problem. For 100 rows nad 5 columns, I receive this error:&amp;nbsp;&lt;STRONG&gt;ERROR: (execution) Matrices do not conform to the operation.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Please see below for code that generates some random data - I'm still not fully familiar with&amp;nbsp;ELEMENT function, but I can't find anything wrong with the&amp;nbsp;code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
	
	rows = 100;
	cols = 5; 

	min = 100;
	max = 1000;
	call randseed(555);

	a = j(rows,cols,.); 
	call randgen(a, "Uniform"); /*  ~ U(0,1) */
	a = min + floor( (1+Max-Min)*a );
	
	x1 = min:max;
	x1 = shape(x1,max-min+1,1);
	x2 = j(max-min+1,1,.);
	call randgen(x2, "Uniform"); /*  ~ U(0,1) */
	x2 = 10 + floor( (1+50-10)*x2 );

	b = x1||x2;


	/*vectorization*/
	c = j(nrow(a),ncol(a),.);

	do i = 1 to nrow(a);
		idx = loc( element(b[,1], a[i,]) ); /* location of elements in b[,1] that match a[i,] */
		c[i,] = T( b[idx,2] );     /* copy elements from b[,2] */
	end;

quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Aug 2017 13:38:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorized-matrix-look-up/m-p/385295#M3676</guid>
      <dc:creator>MDaniel</dc:creator>
      <dc:date>2017-08-03T13:38:05Z</dc:date>
    </item>
    <item>
      <title>Re: Vectorized matrix look-up</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorized-matrix-look-up/m-p/385297#M3677</link>
      <description>&lt;P&gt;I think Rick's solution assumed that each row of the matrix a, has unique key&amp;nbsp;values that are strictly increasing.&amp;nbsp; If that's likely to be true for&amp;nbsp;your real data set, then you may still be OK.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2017 13:43:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorized-matrix-look-up/m-p/385297#M3677</guid>
      <dc:creator>IanWakeling</dc:creator>
      <dc:date>2017-08-03T13:43:29Z</dc:date>
    </item>
    <item>
      <title>Re: Vectorized matrix look-up</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorized-matrix-look-up/m-p/385299#M3678</link>
      <description>&lt;P&gt;Ian deduced the problem correctly. I assumed that the number of elements that match was always NCOL(a).. In your random data, that assumption does not hold.&amp;nbsp;If you need to handle the case where there are fewer elements that match than there are columns of a, use the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i = 1 to nrow(a);
   idx = loc( element(b[,1], a[i,]) ); /* location of elements in b[,1] that match a[i,] */
   if ncol(idx)&amp;gt;0 then 
      c[i,1:ncol(idx)] = T( b[idx,2] );     /* copy elements from b[,2] */
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you can have MORE elements that match than columns of a, then you either need to truncate the results to NCOL(a) elements of make some other modification.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2017 13:48:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorized-matrix-look-up/m-p/385299#M3678</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-08-03T13:48:41Z</dc:date>
    </item>
    <item>
      <title>Re: Vectorized matrix look-up</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorized-matrix-look-up/m-p/385300#M3679</link>
      <description>&lt;P&gt;I've identified the problem - if a value appears more than once per line in matrix 'a', the error will be generated. The real dataset definitely has distinct values per row, so this will work.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2017 13:49:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorized-matrix-look-up/m-p/385300#M3679</guid>
      <dc:creator>MDaniel</dc:creator>
      <dc:date>2017-08-03T13:49:53Z</dc:date>
    </item>
  </channel>
</rss>

