s-news
[Top] [All Lists]

finding elements in an array

To: s-news list <s-news@lists.biostat.wustl.edu>
Subject: finding elements in an array
From: Carlisle Thacker <thacker@aoml.noaa.gov>
Date: Tue, 26 Jun 2001 16:31:48 -0400 (EDT)
Suppose you have two vectors, which contain row and column indices of
some elements of an array.  Is there a good way to find the
corresponding elements?

For example:

> test <- matrix(1:20, ncol=4)
> test.i <- c(1,3,5)
> test.j <- c(4,2,2)

Is there a better way to get this answer?
> diag(test[test.i,test.j])
[1] 16  8 10

Thanks,

Carlisle


********************************************************
* William Carlisle Thacker                             *
*                                                      *
* Atlantic Oceanographic and Meteorological Laboratory *
* 4301 Rickenbacker Causeway                           *
* Miami, Florida 33149 USA                             *
* Office:  (305) 361-4323                              *
* Fax:     (305) 361-4392                              *
********************************************************


<Prev in Thread] Current Thread [Next in Thread>
  • finding elements in an array, Carlisle Thacker <=