s-news
[Top] [All Lists]

Re: Extracting Elements from a matrix

To: "Dave Krantz" <dhk@paradox.psych.columbia.edu>
Subject: Re: Extracting Elements from a matrix
From: "Biggerstaff, Brad J. \(CDC/CCID/NCZVED\)" <bkb5@cdc.gov>
Date: Wed, 7 Nov 2007 14:13:15 -0700
Cc: <s-news@lists.biostat.wustl.edu>
References: <20071107184517.CAE3C1C4699@paradox.psych.columbia.edu>
Thread-index: Acghd0zeAi4g9MAASbmsjFaaZQEWwgAB9zSw
Thread-topic: [S] Extracting Elements from a matrix
Well, in fact, I did know that.  And one can extract elements from a
matrix using a dim()-dimensional matrix of logical values, and .... lots
of ways.  I wanted to use a vector.  Your approach is likely faster, I
admit.  

I'll refrain from comment on your "users" versus "programmers" remark.

Brad


-----Original Message-----
From: Dave Krantz [mailto:dhk@paradox.psych.columbia.edu] 
Sent: Wednesday, November 07, 2007 11:45 AM
To: Biggerstaff, Brad J. (CDC/CCID/NCZVED)
Cc: dhk@paradox.psych.columbia.edu
Subject: Re: [S] Extracting Elements from a matrix

What you don't know is that elements of a matrix can be extracted by
using a two-column matrix index, the first column specifying the row and
the second the column.

Thus, your problem is solved by setting Index <- cbind(1:5, pos) and
then matrix.name[Index].

This is a feature of S (and R) not widely known among users, though it
is well known by programmers.

Dave Krantz (dhk@columbia.edu)


<Prev in Thread] Current Thread [Next in Thread>