creating a certain type of matrix

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

creating a certain type of matrix

Taka Matzmoto
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 matrix will be 40*5
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) and then the rest of elements
of the third column will be zeros.

The first half of the fourth column of the matrix will be zeors.The first
TEN elements of the second half of the fourth column will be zeros too and
then 5 random numbers coming from rnorm(1,0,1) and then the rest of elements
of the third column will be zeros.

The first half of the fifth column of the matrix will be zeors.The first
FIFTEEN elements of the second half of the fourth column will be zeros too
and then 5 random numbers coming from rnorm(1,0,1).

I tried to create 10 different functions ( one for 10, 20, 30, 40, .... ,
100 variables) but it's not efficient.

Any help or advice for creating one function that can do all 10 kind of
variable cases would be appreciated.

Thans in advance

Taka

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: creating a certain type of matrix

PIKAL Petr
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" <[hidden email]>
To:             [hidden email]
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 matrix will be 40*5
> 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) and then
> the rest of elements of the third column will be zeros.
>
> The first half of the fourth column of the matrix will be zeors.The
> first TEN elements of the second half of the fourth column will be
> zeros too and then 5 random numbers coming from rnorm(1,0,1) and then
> the rest of elements of the third column will be zeros.
>
> The first half of the fifth column of the matrix will be zeors.The
> first FIFTEEN elements of the second half of the fourth column will be
> zeros too and then 5 random numbers coming from rnorm(1,0,1).
>
> I tried to create 10 different functions ( one for 10, 20, 30, 40,
> .... , 100 variables) but it's not efficient.
>
> Any help or advice for creating one function that can do all 10 kind
> of variable cases would be appreciated.
>
> Thans in advance
>
> Taka
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
[hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html