<?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: Matrix condition in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Matrix-condition/m-p/793373#M5779</link>
    <description>&lt;P&gt;LOC returns indices into the matrix, so you need to be careful if you are comparing elements of a matrix to elements of a vector. Look at this example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
v1 = {1 2 3 -4 5, 
      0 0 0 0 0 ,
      3 4 5 6 7};
lb = {2 1 0  0 0};

/* indicator matrix */
M = (v1 &amp;lt; lb);
print M;
idx = loc(v1 &amp;lt; lb);        /* LOC returns numbers in 1..(nrow(v1)*(ncol(v1)) */
print idx;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can either loop over the rows of the matrix, or you can use the "elementwise maximum operator," which works for matrix-vector comparisons:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* max index operator */
T = (v1 &amp;lt;&amp;gt; lb);
print T;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 30 Jan 2022 15:49:21 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2022-01-30T15:49:21Z</dc:date>
    <item>
      <title>Matrix condition</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Matrix-condition/m-p/793328#M5771</link>
      <description>&lt;P&gt;How to do a boundary condition in a matrix?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example :&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/*handling boundary violations*/
	if X[,1:p]&amp;lt;lb then X[,1:p]=lb;
	else e=0;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I have found some results with any(X)&amp;lt;lb&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I absolutly want to have the row that is &amp;lt;lb assign to lb (and not for all of the lines). As it is a boundary condition.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jan 2022 00:25:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Matrix-condition/m-p/793328#M5771</guid>
      <dc:creator>CerditoSalvaje</dc:creator>
      <dc:date>2022-01-30T00:25:11Z</dc:date>
    </item>
    <item>
      <title>Re: Matrix condition</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Matrix-condition/m-p/793336#M5773</link>
      <description>Then you need get its index firstly .&lt;BR /&gt;&lt;BR /&gt;if any(X[,1:p]&amp;lt;lb) then do;&lt;BR /&gt;  idx=loc(X[,1:p]&amp;lt;lb);&lt;BR /&gt;  X[idx ,1:p]=lb;&lt;BR /&gt;end;</description>
      <pubDate>Sun, 30 Jan 2022 08:19:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Matrix-condition/m-p/793336#M5773</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-01-30T08:19:47Z</dc:date>
    </item>
    <item>
      <title>Re: Matrix condition</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Matrix-condition/m-p/793344#M5775</link>
      <description>&lt;P&gt;Example data is always appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you ask questions, be sure to indicate what symbols represent vectors/matrices versus scalars. I think most people who are reading your question will assume that lb is a scalar value, but I recall from a previous question that lb is a vector.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need to look carefully at your IF statement to make sure it is correct. I think the left-hand side and right-hand side are both vectors. That is, the statement is&lt;/P&gt;
&lt;P&gt;IF v1 &amp;lt; v2 THEN ...&lt;BR /&gt;where both v1 and v2 are row vectors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you make a logical decision with vectors, you should specify whether you are testing for ANY element satisfying the condition or ALL elements satisfying it.&amp;nbsp; &lt;A href="https://blogs.sas.com/content/iml/2012/03/26/any-versus-all-testing-the-elements-of-a-vector.html" target="_self"&gt;By default, SAS/IML uses ALL elements&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Look at the following example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;v1 = {1 2 3 -4 5};&lt;BR /&gt;lb = {2 1 0 0 0};&lt;BR /&gt;if v1 &amp;lt; lb then v1 = lb;&lt;BR /&gt;print v1;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I suspect you only want to replace the elements that exceed the bounds. As KSharp says, you can use LOC for this, but you need to use the resulting index on BOTH SIDES of the assigment:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;v1 = {1 2 3 -4 5};
lb = {2 1 0  0 0};
idx = loc(v1 &amp;lt; lb);
if ncol(idx)&amp;gt;0 then        /* were there any? */
   v1[ ,idx] = lb[ ,idx];  /* assign ONLY elements that satisfied the condition */
print v1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That describes how to solve these problems in general.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the special problem of choosing the elementwise minimum between two vectors, SAS/IML has a special &lt;A href="https://blogs.sas.com/content/iml/2014/12/15/elementwise-minmax-operators.html" target="_self"&gt;"elementwise minimum operator" ("&amp;gt;&amp;lt;")&lt;/A&gt; which reduces the problem to a one-liner:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;v1 = {1 2 3 -4 5};
lb = {2 1 0  0 0};
v1 = v1 &amp;lt;&amp;gt; lb;      /* max index operator */
print v1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jan 2022 11:18:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Matrix-condition/m-p/793344#M5775</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-01-30T11:18:04Z</dc:date>
    </item>
    <item>
      <title>Re: Matrix condition</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Matrix-condition/m-p/793345#M5776</link>
      <description>&lt;P&gt;PS: As you see from my response, you do NOT need an ELSE statement for every IF statement. So you can delete all your statements &lt;STRONG&gt;else e=0; &lt;/STRONG&gt;unless they are useful to you.&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jan 2022 11:22:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Matrix-condition/m-p/793345#M5776</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-01-30T11:22:03Z</dc:date>
    </item>
    <item>
      <title>Re: Matrix condition</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Matrix-condition/m-p/793366#M5777</link>
      <description>&lt;P&gt;Thank you Rick. And what if v1 is a matrix ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc iml;
v1 = {1 2 3 -4 5, 0 0 0 0 0 ,3 4 5 6 7};
lb = {2 1 0  0 0};
idx = loc(v1 &amp;lt; lb);
if ncol(idx)&amp;gt;0 then        /* were there any? */
   v1[ ,idx] = lb[ ,idx];  /* assign ONLY elements that satisfied the condition */
print idx; /*problem it is not in a matrix format*/&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 30 Jan 2022 14:18:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Matrix-condition/m-p/793366#M5777</guid>
      <dc:creator>CerditoSalvaje</dc:creator>
      <dc:date>2022-01-30T14:18:42Z</dc:date>
    </item>
    <item>
      <title>Re: Matrix condition</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Matrix-condition/m-p/793373#M5779</link>
      <description>&lt;P&gt;LOC returns indices into the matrix, so you need to be careful if you are comparing elements of a matrix to elements of a vector. Look at this example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
v1 = {1 2 3 -4 5, 
      0 0 0 0 0 ,
      3 4 5 6 7};
lb = {2 1 0  0 0};

/* indicator matrix */
M = (v1 &amp;lt; lb);
print M;
idx = loc(v1 &amp;lt; lb);        /* LOC returns numbers in 1..(nrow(v1)*(ncol(v1)) */
print idx;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can either loop over the rows of the matrix, or you can use the "elementwise maximum operator," which works for matrix-vector comparisons:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* max index operator */
T = (v1 &amp;lt;&amp;gt; lb);
print T;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jan 2022 15:49:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Matrix-condition/m-p/793373#M5779</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-01-30T15:49:21Z</dc:date>
    </item>
  </channel>
</rss>

