Re: [R] (second round) creating a certain type of matrix
I cleaned up your function a bit but please double check generate.matrix - function(nr, runs=5){ h - nr/2## half of nr nc - nr/10 + 1 mat - matrix(0, nr, nc) ## initialize mat[ ,1] - c( rep(1, h), rnorm(h) ) ## 1st column mat[ (h+1):(h+5), 2] - rnorm(5) ## 2nd column if( nc 3 ){ for (i in 3:nc){ ## column 3 - end start - h + 5*(i-2) + 1 end - start + runs - 1 mat[ start:end, i] - rnorm( runs ) } } return(mat) } However you can simplify this greatly. If you ignore the first column (which looks like some initialisation column in simulation process), then you have a matrix with nr/2 rows and nr/10 columns with diagonal blocks 5 runs filled with rnorm values. Here is what I propose : gen.mat - function(x, runs=5){ if( (x %% 2*runs)!=0 ) stop(x, is not a multiple of , 2*runs) nr - x/2 nc - x/(2*runs) mat - matrix(0, nr, nc) for (i in 1:nc) mat[ ((i-1)*runs + 1) : (i*runs), i ] - rnorm(runs) down - cbind( rnorm(nr), mat ) top - cbind( 1, matrix( 0, nr=nr, nc=nc ) ) out - rbind( top, down ) return(out) } # Examples gen.mat(50) gen.mat(55) ## should generate an error gen.mat(24, runs=6) Does this function do what you want to ? Regards, Adai On Tue, 2006-02-07 at 11:03 -0600, Taka Matzmoto wrote: Hi R users Here is what I got with help from Petr Pikal (Thanks Petr Pikal). I modified Petr Pikal's code to a little to meet my purpose. I created a function to generate a matrix generate.matrix-function(n.variable) { mat-matrix(0,n.variable,(n.variable/2)/5+1) #matrix of zeroes dd-dim(mat) # actual dimensions mat[1:(dd[1]/2),1]-1 #put 1 in first half of first column mat[((dd[1]/2)+1):dd[1],1]-rnorm(dd[1]/2,0,1) #put random numbers in following part of the matrix column 1 mat[((dd[1]/2)+1):((dd[1]/2)+5),2]-rnorm(5,0,1) #put random numbers in column2 for (i in 3:(dd[2])) { length.of.rand.numbers - 5 my.rand.num- rnorm(length.of.rand.numbers, 0,1) start - dd[1]/2+5*(i-2)+1 end - start + length.of.rand.numbers-1 mat[((start):end), i]- my.rand.num } mat } Do you (any R users) have any suggestion to this function to make this function work better or efficiently? Taka It works but I From: Petr Pikal [EMAIL PROTECTED] To: Taka Matzmoto [EMAIL PROTECTED],r-help@stat.math.ethz.ch Subject: Re: [R] creating a certain type of matrix Date: Tue, 07 Feb 2006 08:58:59 +0100 MIME-Version: 1.0 Received: from mail.precheza.cz ([80.188.29.243]) by bay0-mc8-f13.bay0.hotmail.com with Microsoft SMTPSVC(6.0.3790.211); Mon, 6 Feb 2006 23:59:02 -0800 Received: from localhost (localhost [127.0.0.1])by mail.precheza.cz (Mailer) with ESMTP id A636C34E584;Tue, 7 Feb 2006 08:59:00 +0100 (CET) Received: from mail.precheza.cz ([127.0.0.1])by localhost (mail.precheza.cz [127.0.0.1]) (amavisd-new, port 10024)with LMTP id 28608-02-30; Tue, 7 Feb 2006 08:58:59 +0100 (CET) Received: from n1en1.precheza.cz (smtp.precheza.cz [192.168.210.31])by mail.precheza.cz (Mailer) with ESMTP id 35E8634E582;Tue, 7 Feb 2006 08:58:59 +0100 (CET) Received: from pikal ([192.168.210.65]) by n1en1.precheza.cz (Lotus Domino Release 6.5.4FP2) with ESMTP id 2006020708585800-252 ; Tue, 7 Feb 2006 08:58:58 +0100 X-Message-Info: JGTYoYF78jEHjJx36Oi8+Z3TmmkSEdPtfpLB7P/ybN8= X-Confirm-Reading-To: Petr Pikal [EMAIL PROTECTED] X-pmrqc: 1 Return-Receipt-To: Petr Pikal [EMAIL PROTECTED] Priority: normal X-mailer: Pegasus Mail for Windows (4.21c) X-MIMETrack: Itemize by SMTP Server on SRVDomino/PRECHEZA(Release 6.5.4FP2 | September 26, 2005) at 07.02.2006 08:58:58,Serialize by Router on SRVDomino/PRECHEZA(Release 6.5.4FP2 | September 26, 2005) at 07.02.2006 08:58:58,Serialize complete at 07.02.2006 08:58:58 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at precheza.cz Return-Path: [EMAIL PROTECTED] X-OriginalArrivalTime: 07 Feb 2006 07:59:03.0289 (UTC) FILETIME=[5C87D690:01C62BBC] Hi as only you know perfectly which halves and other portions of your matrices contain zeroes and which contain random numbers you has to finalize the function yourself. Here are few ideas. n-20 mat-matrix(0,n,(n/2)/5+1) #matrix of zeroes dd-dim(mat) # actual dimensions mat[1:(dd[1]/2),1]-1 #put 1 in first half of first column mat[((dd[1]/2)+1):dd[1],1]-rnorm(dd[1]/2,0,1) #put random numbers in following part of the matrix column 1 mat[((dd[1]/2)+1):(dd[1]/2)+dd[1]/4,2]-rnorm(dd[1]/4,0,1) #put random numbers in column2 than according to n and dd values you can put any numbers anywhere in your matrix e.g. in for loop (not.tested :-) for (i in 3:dd[2]) { arrange everything into following desired columns e.g. length.of.rand.numbers - (i-2)*5 my.rand.num-
[R] (second round) creating a certain type of matrix
Hi R users Here is what I got with help from Petr Pikal (Thanks Petr Pikal). I modified Petr Pikal's code to a little to meet my purpose. I created a function to generate a matrix generate.matrix-function(n.variable) { mat-matrix(0,n.variable,(n.variable/2)/5+1) #matrix of zeroes dd-dim(mat) # actual dimensions mat[1:(dd[1]/2),1]-1 #put 1 in first half of first column mat[((dd[1]/2)+1):dd[1],1]-rnorm(dd[1]/2,0,1) #put random numbers in following part of the matrix column 1 mat[((dd[1]/2)+1):((dd[1]/2)+5),2]-rnorm(5,0,1) #put random numbers in column2 for (i in 3:(dd[2])) { length.of.rand.numbers - 5 my.rand.num- rnorm(length.of.rand.numbers, 0,1) start - dd[1]/2+5*(i-2)+1 end - start + length.of.rand.numbers-1 mat[((start):end), i]- my.rand.num } mat } Do you (any R users) have any suggestion to this function to make this function work better or efficiently? Taka It works but I From: Petr Pikal [EMAIL PROTECTED] To: Taka Matzmoto [EMAIL PROTECTED],r-help@stat.math.ethz.ch Subject: Re: [R] creating a certain type of matrix Date: Tue, 07 Feb 2006 08:58:59 +0100 MIME-Version: 1.0 Received: from mail.precheza.cz ([80.188.29.243]) by bay0-mc8-f13.bay0.hotmail.com with Microsoft SMTPSVC(6.0.3790.211); Mon, 6 Feb 2006 23:59:02 -0800 Received: from localhost (localhost [127.0.0.1])by mail.precheza.cz (Mailer) with ESMTP id A636C34E584;Tue, 7 Feb 2006 08:59:00 +0100 (CET) Received: from mail.precheza.cz ([127.0.0.1])by localhost (mail.precheza.cz [127.0.0.1]) (amavisd-new, port 10024)with LMTP id 28608-02-30; Tue, 7 Feb 2006 08:58:59 +0100 (CET) Received: from n1en1.precheza.cz (smtp.precheza.cz [192.168.210.31])by mail.precheza.cz (Mailer) with ESMTP id 35E8634E582;Tue, 7 Feb 2006 08:58:59 +0100 (CET) Received: from pikal ([192.168.210.65]) by n1en1.precheza.cz (Lotus Domino Release 6.5.4FP2) with ESMTP id 2006020708585800-252 ; Tue, 7 Feb 2006 08:58:58 +0100 X-Message-Info: JGTYoYF78jEHjJx36Oi8+Z3TmmkSEdPtfpLB7P/ybN8= X-Confirm-Reading-To: Petr Pikal [EMAIL PROTECTED] X-pmrqc: 1 Return-Receipt-To: Petr Pikal [EMAIL PROTECTED] Priority: normal X-mailer: Pegasus Mail for Windows (4.21c) X-MIMETrack: Itemize by SMTP Server on SRVDomino/PRECHEZA(Release 6.5.4FP2 | September 26, 2005) at 07.02.2006 08:58:58,Serialize by Router on SRVDomino/PRECHEZA(Release 6.5.4FP2 | September 26, 2005) at 07.02.2006 08:58:58,Serialize complete at 07.02.2006 08:58:58 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at precheza.cz Return-Path: [EMAIL PROTECTED] X-OriginalArrivalTime: 07 Feb 2006 07:59:03.0289 (UTC) FILETIME=[5C87D690:01C62BBC] Hi as only you know perfectly which halves and other portions of your matrices contain zeroes and which contain random numbers you has to finalize the function yourself. Here are few ideas. n-20 mat-matrix(0,n,(n/2)/5+1) #matrix of zeroes dd-dim(mat) # actual dimensions mat[1:(dd[1]/2),1]-1 #put 1 in first half of first column mat[((dd[1]/2)+1):dd[1],1]-rnorm(dd[1]/2,0,1) #put random numbers in following part of the matrix column 1 mat[((dd[1]/2)+1):(dd[1]/2)+dd[1]/4,2]-rnorm(dd[1]/4,0,1) #put random numbers in column2 than according to n and dd values you can put any numbers anywhere in your matrix e.g. in for loop (not.tested :-) for (i in 3:dd[2]) { arrange everything into following desired columns e.g. length.of.rand.numbers - (i-2)*5 my.rand.num- rnorm(length.of.rand.numbers, 0,1) start - dd[1]/2+dd[1]/4 end - start + length.of.rand.numbers mat[start:end, i]- my.rand.num } HTH Petr On 7 Feb 2006 at 0:07, Taka Matzmoto wrote: From: Taka Matzmoto [EMAIL PROTECTED] To:r-help@stat.math.ethz.ch Date sent: Tue, 07 Feb 2006 00:07:11 -0600 Subject: [R] creating a certain type of matrix Hi R users I like to generate a certain type of matrix. If there are 10 variables, the matrix will have nrow=10, ncol=((10/2))/5+1. so the resulting matrix's dimension 10 by 2. If there are 50 variables the dimension of the resulting matrix will be 50 by 6. The arrangement of elements of this matrix is important to me and I can't figure out how to arrange elements. If I have 20 variables. The resulting matrix will be 20 by 3 The first half of first column of the matrix will be 1s. The all elements on the second half of the first column of the matrix will be random numbers coming from rnorm(1,0,1). The first half of the second column of the matrix will be zeros. The first five elements of the second half of the second column of the matrix will be random numbers coming from rnorm(1,0,1). After that, the remaining elements of the second half will be zeros. The first half of the third column of the matrix will be zeors. The first five elements of the second half of the third column will be zeros too and then 5 random numbers coming from rnorm(1,0,1). If there are 40 variables the resulting