[R] locating element in distance matrix

Berend Hasselman bhh at xs4all.nl
Fri Jan 11 19:08:56 CET 2013


On 11-01-2013, at 18:55, eliza botto <eliza_botto at hotmail.com> wrote:

> 
> Dear useRs,
> I have a very basic question. I have a distance matrix and i skipped the upper part of it deliberately. The distance matrix is 1000*1000.  Then i used "min" command to extract the lowest value from that matrix. Now i want to know what is the location of that lowest element? More precisely, the row and column number of that lowest element. 

If you had looked at the help of min you could have seen that which.min could be useful.

?which.min

and from See also

?arrayInd

so this will do it

mat <- matrix(runif(25),nrow=5)
arrayInd(which.min(mat),.dim=dim(mat))

Berend



More information about the R-help mailing list