Hi:

Here are some functions for computing elementary matrices so that you can do
Gauss elimination the hard way (the easy way is the LU decomposition, but I
digress....)  I wouldn't use these for serious work, since there are no
checks for matrix instability, but the essential ideas are there. It is
assumed that you know the relationship between elementary matrices, the
original matrix and its Gauss reduced form.

Eij <- function(p, i, j) {
   # Permutation matrix that obtains by exchanging rows i and j
   E <- diag(rep(1, p))
   E[j, i] <- E0[i,j] <- 1
   E[j, j] <- E0[i, i] <- 0
   E
  }


EiC <- function(p, i, c) {
   # Elementary matrix obtained by multiplying i-th row of I by c
  if(c == 0L) stop ('cannot multiply row by zero')
  E <- diag(rep(1, p))
  E[i, ] <- c * E0[i, ]
  E
  }

EijC <- function(p, i, j, c) {
  # Elementary matrix obtained by multiplying c * row j and
  # adding it to the i-th row of E
  E <- diag(rep(1, p))
  E[i, ] <- E[i, ] + c * E[j, ]
  E
  }

To answer your question,

> A=matrix(1:16,ncol=4)
> A
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16
> EijC(4, 3, 1, 2) %*% A  # Add 2 * R1 of A to R3
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14
[3,]    5   17   29   41
[4,]    4    8   12   16

To reduce the first column of A:
> EijC(4, 2, 1, -2) %*% EijC(4, 3, 1, -3) %*% EijC(4, 4, 1, -4) %*% A
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    0   -4   -8  -12
[3,]    0   -8  -16  -24
[4,]    0  -12  -24  -36


HTH,
Dennis

On Fri, Aug 27, 2010 at 10:32 PM, Cheng Peng <cpeng@usm.maine.edu> wrote:

>
> Sorry for possible misunderstanding:
>
> I want to define a matrix (B) based on an existing matrix (A) in a single
> step and keep A unchanged:
>
> > #Existing matrix
> > A=matrix(1:16,ncol=4)
> > A
>     [,1] [,2] [,3] [,4]
> [1,]    1    5    9   13
> [2,]    2    6   10   14
> [3,]    3    7   11   15
> [4,]    4    8   12   16
> > # New matrix B is defined to be the submatrix after row1 and column1 are
> > deleted.
> > B=A[-1,-1]    # this single step deletes row1 nad column 1 and assigns
> the
> > name to the resulting submatrix.
> > B                 # check the new matrix B
>      [,1] [,2] [,3]
> [1,]    6   10   14
> [2,]    7   11   15
> [3,]    8   12   16
> > A                 # check the original matrix A
>      [,1] [,2] [,3] [,4]
> [1,]    1    5    9   13
> [2,]    2    6   10   14
> [3,]    3    7   11   15
> [4,]    4    8   12   16
>
>
> Question: How can I do define a new matrix (D) by adding 2*row1 to row3 in
> A
> in a single step as what was done in the above example?
>
> If you do:  A[3,]=2*A[1,]+A[3,],  the new A is not the original A; if you
> D=A first, then D[3,]=2*D[1,]+D[3,], you used two step!
>
> Hope this clarifies my original question. Thanks again.
>
>
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/How-to-define-new-matrix-based-on-an-elementary-row-operation-in-a-single-step-tp2341768p2352538.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]

