s-news
[Top] [All Lists]

Re: matrix manipulation question

To: Felipe <Felipe.Jordao@moodys.com>
Subject: Re: matrix manipulation question
From: David L Lorenz <lorenz@usgs.gov>
Date: Wed, 20 Jul 2005 11:02:36 -0500
Cc: s-news@lists.biostat.wustl.edu, s-news-owner@lists.biostat.wustl.edu
In-reply-to: <CDBE3ADAFCB39F41A19FF2BE650978FF3D041E@mdynycmsx03.ad.moodys.net>

Felipe,
  Use apply instead of a for loop.  Here's an example of a function that should work. Note that apply returns a matrix that would need to be transposed.

apply(matrix(c(
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,1,2,3,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,
4,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2,3,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,2,3,4,5,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,1,2,3,4,5,0,0,0,0,0,0), ncol=17, byrow=T), 1,
FUN = function(x) {
        one <- match(1, x, nomatch=0)
        return(c(rep(1,one), rep(0,length(x)-one)))
})

Dave


Jordão, Felipe <Felipe.Jordao@moodys.com>
Sent by: s-news-owner@lists.biostat.wustl.edu

07/20/2005 10:16 AM

       
        To:        <s-news@lists.biostat.wustl.edu>
        cc:        
        Subject:        [S] matrix manipulation question




Hi all,

I have a matrix with some numbers (0:5). The number sequence usually begins with 1, but if starts in column 1 it can begin with another number. Once started it will go sequentially with no 0s in the middle, then go back to 0s. I want to do the following for each row:

1. If the sum of the row is zero, leave it as all 0s (i.e. it's blank to begin with, then leave it blank)
2. If the sum of the row is not zero, continue and do the following
3. Fill it with 1s until I hit the first "1", fill it with zeroes thereafter

Here is an example:

Start Matrix
00000000000000000
00001230000000000
00000000100000000
45000000000000000
23400000000000000
12345000000000000
00000012345000000
...

End Matrix
00000000000000000
11111000000000000
11111111100000000
00000000000000000
00000000000000000
10000000000000000
11111110000000000
...

It is a large enough matrix that I get a memory error if I use for loops (about 30x10000). I also need to keep the ordering of the rows, so if I start doing things like breaking it up in chunks it has to be put back properly.

I appreciate any suggestions.

thanks in advance,
Felipe




---------------------------------------

The information contained in this e-mail message, and any attachment thereto, is confidential and may not be disclosed without our express permission.  If you are not the intended recipient or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution or copying of this message, or any attachment thereto, in whole or in part, is strictly prohibited.  If you have received this message in error, please immediately notify us by telephone, fax or e-mail and delete the message and all of its attachments.  Thank you.

Every effort is made to keep our network free from viruses.  You should, however, review this e-mail message, as well as any attachment thereto, for viruses.  We take no responsibility and have no liability for any computer virus which may be transferred via this e-mail message.

--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news

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