<?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: taking the minimum of a matrix in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/taking-the-minimum-of-a-matrix/m-p/793329#M5772</link>
    <description>&lt;P&gt;Thank you for your answer. In fact, I can show you my code. And I will try to explain it better, sorry.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a matrix with 3 columns and 20 lines. The first column is the value for X, the second one for Y and the third one is f(X,Y), the image of x and y by a function f. I want the minimum of the function, then it is the minimum of the third column. This is easy, it is min(X[,p+1]). With p the number of variables (2) and all the lines. And I have the result which is the minimum. BUT I don't have the values for the other columns for this minimum. I would like to have the entire row where the value of the image is minimum.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here the code. In this case we are talking about gbest and pbest.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc iml;
xmin=0;     /* min has to be &amp;gt;= 0 or else x^(1/4) and x^(3/4) are undefined */
xmax=100;
/***parameters and variables***/
n=20;			   			/*number of particles*/
p=2;
maxt=1000;					/*maximum iteration*/
maxrun=1;					/*trials of the algorithm*/
X=j(n,p+1,0);				/*initialization of the particle's matrix*/
lb=j(1,p,xmin);
ub=j(1,p,xmax);

/* vectorization of f: R^2 --&amp;gt; R */
start f(x);
   con=100*x[,1]+200*x[,2]-1500;
   fc = 3*(x[,1]##0.25) # (x[,2]##0.75);
   pen=10**9;
   idx = loc(con &amp;gt; 0);
   if ncol(type)&amp;gt;0 then 
		fc[idx] = pen*con[idx];
   return(fc);
finish f;

call randseed(1234);
Y = j(n,P);/* allocate for random values */
do run=1 to maxrun; /*this line is not important in this example*/
   /***initialization of the pso***/
   call randgen(Y, 'uniform');

	/*sample in a uniform distribution to initialize the particles position*/
   X[,1:p] = lb + (ub-lb)#Y;
	/*to fill the third column of X (which corresponds to the output of the function), we evaluate the fitting for each particule*/
	X[,p+1]= f(X);

pbest=X;			/*initialization of pbest so we can iterate then*/
V=0.1*X;
/*we find the current minimum of f, given the current values of X (if we want to minimize the function)*/	
titi=min(pbest[,p+1]);
/*then we iterate to assign the coordinates of this minimum to gbest*/
	do i=1 to n;
		if pbest[i,p+1]=titi then gbest=pbest[i,];
		else e=0;
	end;
end;
print gbest;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I would like to take away the titi and all this stuff.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 30 Jan 2022 00:45:44 GMT</pubDate>
    <dc:creator>CerditoSalvaje</dc:creator>
    <dc:date>2022-01-30T00:45:44Z</dc:date>
    <item>
      <title>taking the minimum of a matrix</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/taking-the-minimum-of-a-matrix/m-p/793322#M5769</link>
      <description>&lt;P&gt;Hi everybody, I will explain my problem :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a matrix with X and Y and the image of both by a funcion. Then I have 3 columns.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want the minimum image of the function but keeping X and Y.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have found the result but it is no so clean. I think that there is a better option.&amp;nbsp;Look at the way I found it :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;titi=min(pbest[,p+1]);

	/*then we iterate to assign the coordinates of this minimum to gbest*/
	do i=1 to n;
		if pbest[i,p+1]=titi then gbest=pbest[i,];
		else e=0;
	end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and this is another option I have tried :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;titi=min(ncol(pbest[,p+1]));
print titi;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The idea is that I need what I have in the Row which has the minimum value in the column p+1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks.&lt;/P&gt;</description>
      <pubDate>Sat, 29 Jan 2022 23:54:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/taking-the-minimum-of-a-matrix/m-p/793322#M5769</guid>
      <dc:creator>CerditoSalvaje</dc:creator>
      <dc:date>2022-01-29T23:54:56Z</dc:date>
    </item>
    <item>
      <title>Re: taking the minimum of a matrix</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/taking-the-minimum-of-a-matrix/m-p/793327#M5770</link>
      <description>&lt;P&gt;You can find the minimum of a column (or minimum of a matrix, or minimum of a row) by using subscript reduction operators. &lt;A href="https://documentation.sas.com/doc/en/imlug/15.2/imlug_workmatrix_sect025.htm" target="_blank" rel="noopener"&gt;https://documentation.sas.com/doc/en/imlug/15.2/imlug_workmatrix_sect025.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's really hard to visualize what the matrix is that you have and what matrix you want by reading your description in words.&amp;nbsp;Also, at one point you say "minimum of a matrix" and at other times you appear to want the minimum of a column. It would help greatly if you could provide a small example.&amp;nbsp; Show us what you have and what you want.&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jan 2022 00:30:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/taking-the-minimum-of-a-matrix/m-p/793327#M5770</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-01-30T00:30:16Z</dc:date>
    </item>
    <item>
      <title>Re: taking the minimum of a matrix</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/taking-the-minimum-of-a-matrix/m-p/793329#M5772</link>
      <description>&lt;P&gt;Thank you for your answer. In fact, I can show you my code. And I will try to explain it better, sorry.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a matrix with 3 columns and 20 lines. The first column is the value for X, the second one for Y and the third one is f(X,Y), the image of x and y by a function f. I want the minimum of the function, then it is the minimum of the third column. This is easy, it is min(X[,p+1]). With p the number of variables (2) and all the lines. And I have the result which is the minimum. BUT I don't have the values for the other columns for this minimum. I would like to have the entire row where the value of the image is minimum.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here the code. In this case we are talking about gbest and pbest.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc iml;
xmin=0;     /* min has to be &amp;gt;= 0 or else x^(1/4) and x^(3/4) are undefined */
xmax=100;
/***parameters and variables***/
n=20;			   			/*number of particles*/
p=2;
maxt=1000;					/*maximum iteration*/
maxrun=1;					/*trials of the algorithm*/
X=j(n,p+1,0);				/*initialization of the particle's matrix*/
lb=j(1,p,xmin);
ub=j(1,p,xmax);

/* vectorization of f: R^2 --&amp;gt; R */
start f(x);
   con=100*x[,1]+200*x[,2]-1500;
   fc = 3*(x[,1]##0.25) # (x[,2]##0.75);
   pen=10**9;
   idx = loc(con &amp;gt; 0);
   if ncol(type)&amp;gt;0 then 
		fc[idx] = pen*con[idx];
   return(fc);
finish f;

call randseed(1234);
Y = j(n,P);/* allocate for random values */
do run=1 to maxrun; /*this line is not important in this example*/
   /***initialization of the pso***/
   call randgen(Y, 'uniform');

	/*sample in a uniform distribution to initialize the particles position*/
   X[,1:p] = lb + (ub-lb)#Y;
	/*to fill the third column of X (which corresponds to the output of the function), we evaluate the fitting for each particule*/
	X[,p+1]= f(X);

pbest=X;			/*initialization of pbest so we can iterate then*/
V=0.1*X;
/*we find the current minimum of f, given the current values of X (if we want to minimize the function)*/	
titi=min(pbest[,p+1]);
/*then we iterate to assign the coordinates of this minimum to gbest*/
	do i=1 to n;
		if pbest[i,p+1]=titi then gbest=pbest[i,];
		else e=0;
	end;
end;
print gbest;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I would like to take away the titi and all this stuff.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jan 2022 00:45:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/taking-the-minimum-of-a-matrix/m-p/793329#M5772</guid>
      <dc:creator>CerditoSalvaje</dc:creator>
      <dc:date>2022-01-30T00:45:44Z</dc:date>
    </item>
    <item>
      <title>Re: taking the minimum of a matrix</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/taking-the-minimum-of-a-matrix/m-p/793343#M5774</link>
      <description>&lt;P&gt;In general, you can &lt;A href="https://blogs.sas.com/content/iml/2011/05/16/finding-data-that-satisfy-a-criterion.html" target="_self"&gt;use the LOC function to find rows that satisfy some criterion&lt;/A&gt;.&amp;nbsp; The LOC function is the most important function that new SAS/IML programmers need to discover.&amp;nbsp; Be aware, however, if your matrix has tied values, you might get more than one row that satisfies the criterion. In that case, I assume any row is okay? If so, take the first:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
pbest = {1 2 9,
         4 5 6,
         1 3 10,
         4 1 3,
         1 1 8,
         5 2 3};
p = 2;
n = nrow(pbest);

titi=min(pbest[,p+1]);

/* 1. Use LOC to find row that satisfies criterion */
minIdx = loc(pbest[,p+1]=titi);  
gbest = pbest[minIdx, ];     /* in case of tied values, might get more than one row */
print gbest;

gbest = pbest[minIdx[1], ];  /* if you only want one row, take first */
print gbest;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In this case, the "criterion" is actually a minimum. In SAS/IML,&lt;A href="https://blogs.sas.com/content/iml/2014/12/01/max-and-min-rows-and-cols.html" target="_self"&gt; there is a special operator (the '&amp;gt;:&amp;lt;' subscript reduction operator) that can return the row for which a minimum exists.&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* 2. For the MIN, you can also use v[&amp;gt;:,] to get the index directly */
v = pbest[,p+1];  
minIdx = v[&amp;gt;:&amp;lt;];
print minIdx;
gbest = pbest[minIdx, ];     /* always returns one row */
print gbest;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jan 2022 10:57:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/taking-the-minimum-of-a-matrix/m-p/793343#M5774</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-01-30T10:57:14Z</dc:date>
    </item>
  </channel>
</rss>

