Jonathan Williams
2005-Oct-17 09:32 UTC
[R] how to find indices of particular array elements
Dear R helpers, I have a largish matrix (1300 x 1300) and I wish to find the row and column numbers that identify particular elements whose values I know in advance (for example, the row and column numbers for the maximum value of the matrix). I have looked in the help manual and found the functions 'row' and 'col' for finding the indices of a matrix. But, I can't figure out how to use them to obtain the indices of the matrix element with maximum value, despite over an hour of trying! Many thanks for your help, Jonathan Williams
How about the function which? ?which On 10/17/05, Jonathan Williams <jonathan.williams at pharmacology.oxford.ac.uk> wrote:> Dear R helpers, > > I have a largish matrix (1300 x 1300) and I wish to find the row and column > numbers that identify particular elements whose values I know in advance > (for example, the row and column numbers for the maximum value of the > matrix). > > I have looked in the help manual and found the functions 'row' and 'col' for > finding the indices of a matrix. But, I can't figure out how to use them to > obtain the indices of the matrix element with maximum value, despite over an > hour of trying! > > Many thanks for your help, > > Jonathan Williams > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Xiaohua Dai, Dr. -------------------------------------------------------------------------------- * Postdoctoral in elephant-tree ecosystem simulation --------------------------------------------------------------------------------- Centre for Systems Research, Durban Institute of Technology P.O.Box 953, Durban 4000, South Africa Tel: +27-31-2042737(O) Fax: +27-31-2042736(O) Mobile: +27-723682954
# create a 4x4 matrix with random values smallmatrix = matrix(runif(16)*10, ncol = 4, nrow = 4) # get the row and column number of the item in the matrix with the highest value which(smallmatrix == max(smallmatrix), arr.ind = 1) HTH, Martin --- Jonathan Williams <jonathan.williams at pharmacology.oxford.ac.uk> wrote:> Dear R helpers, > > I have a largish matrix (1300 x 1300) and I wish to > find the row and column > numbers that identify particular elements whose > values I know in advance > (for example, the row and column numbers for the > maximum value of the > matrix). > > I have looked in the help manual and found the > functions 'row' and 'col' for > finding the indices of a matrix. But, I can't figure > out how to use them to > obtain the indices of the matrix element with > maximum value, despite over an > hour of trying! > > Many thanks for your help, > > Jonathan Williams > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >__________________________________ Access over 1 million songs. Try it free.