In general, you can use the LOC function to find rows that satisfy some criterion. The LOC function is the most important function that new SAS/IML programmers need to discover. 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:
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;
In this case, the "criterion" is actually a minimum. In SAS/IML, there is a special operator (the '>:<' subscript reduction operator) that can return the row for which a minimum exists.
/* 2. For the MIN, you can also use v[>:,] to get the index directly */
v = pbest[,p+1];
minIdx = v[>:<];
print minIdx;
gbest = pbest[minIdx, ]; /* always returns one row */
print gbest;
... View more