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. Thanks in advance elisa [[alternative HTML version deleted]]
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
On Fri, Jan 11, 2013 at 11:55 AM, eliza botto <eliza_botto@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. > Thanks in advance > elisa >You didn't provide sample data, but perhaps this will work for you.> x <- matrix(sample(1:100000000, 1000000), 1000, 1000) > min(x)[1] 135> row <- which(apply(x, 1, min) == min(x)) > col <- which(apply(x, 2, min) == min(x)) > x[row, col][1] 135 row and col are obviously the location in the matrix of the min. HTH James [[alternative HTML version deleted]]
On Jan 11, 2013, at 9:55 AM, eliza botto wrote:> > Dear useRs, > I have a very basic question. I have a distance matrix and i skipped > the upper part of it deliberately.I have no idea what htat means. Code is always helpful in resolving ambiguities.> 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. > Thanks in advance?which which( distmat == min(distmat), arr.ind=TRUE) (It's possible to have more than one match and it would be up to you to decide how to break ties.) -- David Winsemius, MD Alameda, CA, USA