[R] How to define new matrix based on an elementary row oper

(Ted Harding) Ted.Harding at manchester.ac.uk
Sat Aug 28 14:11:11 CEST 2010


On 28-Aug-10 11:27:30, Gabor Grothendieck wrote:
> On Sat, Aug 28, 2010 at 1:32 AM, Cheng Peng <cpeng at 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.
> 
> Try using replace:
> 
>    D <- replace(A, row(A) == 3, 2 * A[1,] + A[3,])

That's neat -- and subtle! It's the sort of thing one wouldn't think
of doing until one has seen it done!

  ?replace:
  Replace Values in a Vector
  Description:
    'replace' replaces the values in 'x' with indices given in
    'list' by those given in 'values'. If necessary, the values
    in 'values' are recycled.
  Usage:
    replace(x, list, values)
  Arguments:
         x: vector
      list: an index vector
    values: replacement values
  Value:
    A vector with the values replaced.
  Note:
    'x' is unchanged: remember to assign the result.


One needs to realise that a matrix A is a vector with a dimension
attribute, hence is accessed "sequentially" like any vector
(though displayed as an array). So, with the above matrix A:

  row(A)==3
  #       [,1]  [,2]  [,3]  [,4]
  # [1,] FALSE FALSE FALSE FALSE
  # [2,] FALSE FALSE FALSE FALSE
  # [3,]  TRUE  TRUE  TRUE  TRUE
  # [4,] FALSE FALSE FALSE FALSE

  A[row(A)==3]
  # [1]  3  7 11 15

Then 'replace(A, row(A) == 3, 2 * A[1,] + A[3,])' replaces that
part of the vector A as indexed by TRUE with the given expression
   2 * A[1,] + A[3,]  =   5 17 29 41

Hence
  replace(A, row(A) == 3, 2 * A[1,] + A[3,])
  #      [,1] [,2] [,3] [,4]
  # [1,]    1    5    9   13
  # [2,]    2    6   10   14
  # [3,]    5   17   29   41
  # [4,]    4    8   12   16

is then assigned to D.

Hoping this helps to clarify!
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 28-Aug-10                                       Time: 13:11:08
------------------------------ XFMail ------------------------------



More information about the R-help mailing list