s-news
[Top] [All Lists]

Re: Extracting Elements from a matrix

To: "Ilouga, Pierre" <Pierre.Ilouga@evotec.com>
Subject: Re: Extracting Elements from a matrix
From: "Biggerstaff, Brad J. \(CDC/CCID/NCZVED\)" <bkb5@cdc.gov>
Date: Wed, 7 Nov 2007 11:07:59 -0700
Cc: <s-news@lists.biostat.wustl.edu>
References: <872D12160996E34584346FAF67F200E8027862@ge-ham-sma01.germany.evotecoai.com>
Thread-index: AcghYUBt53Q3oqr2RyWEATzTh8bRugAB4x2A
Thread-topic: [S] Extracting Elements from a matrix
t(mat)[3*(0:4) + pos]
 
(the transpose is required because S accesses matrices in column order when viewed as a vector...you could switch how you define pos and switch the 3 and 4 to avoid this)
 
For generality:  the 3=ncol(mat) and the 0:4 = 0:(nrow(mat) - 1)
 
Brad
 
Brad Biggerstaff, Ph.D.
Mathematical Statistician
Division of Vector-Borne Infectious Diseases
National Center for Zoonotic, Vector-Borne and Enteric Diseases
Centers for Disease Control and Prevention
3150 Rampart Road
Fort Collins, Colorado  80521
(970) 221-6473 ...
BBiggerstaff@cdc.gov
) 221-6473 ... BBiggerstaff@cdc.gov
 


From: s-news-owner@lists.biostat.wustl.edu [mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of Ilouga, Pierre
Sent: Wednesday, November 07, 2007 10:12 AM
To: s-news@lists.biostat.wustl.edu
Subject: [S] Extracting Elements from a matrix

Dear Splus experts,

Does anyone has an idea how to extract some elements from a matrix in an EFFICIENT way? The following example clarifies  my question.

Given a  5x3 matrix  mat <- matrix(1:15, nrow=5, byrow=F)  and a length 5 -vector  of positions  pos <- c(2, 1, 3, 3, 1), the goal is to extract the vector c(6, 2, 13, 14, 5) which  corresponds to the column pos[j]  (j in 1:3) and row i  ( i in 1:5) without using for-loops and other memory consuming functions.

I?ve solve it via lists. However the slowness of the command executions doesn?t make it useful for  monte carlo simulation.

Thanks in advance.

PI

S-PLUS : Copyright (c) 1988, 2002 Insightful Corp.

S : Copyright Lucent Technologies, Inc.

Version 6.1.2 Release 2 for Linux 2.2.12 : 2002

Working data will be in .Data


Dr. Pierre Ilouga

Screening Operations/Discovery Informatics

EVOTEC AG

Schnackenburgallee 114

22525 Hamburg

Germany

pierre.ilouga@evotec.com

www.evotec.com

                   

Evotec AG Hamburg. Amtsgericht Hamburg HRB 68223
Vorstand: Jörn Aldag (Vorsitzender)
Aufsichtsratsvorsitzender: Prof. Dr. Dr. h.c. mult. Heinz Riesenhuber

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