Matrix Partida in R

1

In R there are routines to calculate operations like addition and subtraction, in this case I want to apply them to split matrices or also calls in blocks. Does anyone know what those functions are for adding and multiplying split matrices?

Thank you!

    
asked by jose 20.10.2017 в 22:49
source

1 answer

0

The matrix trimming can be done without the need of functions, simply use the operators [] and indicate the set of rows and columns that we are going to cut. Let's see your example:

We create the matrix:

> m<-matrix(c(1,4,6,7,0,0,1,1,0,0,3,2),3,4, byrow=TRUE)
> m
     [,1] [,2] [,3] [,4]
[1,]    1    4    6    7
[2,]    0    0    1    1
[3,]    0    0    3    2

The first two cuts are made taking row 1 and two columns

> m[1,c(1,2)]

[1] 1 4

> m[1,c(3,4)]

[1] 6 7

Note, that when we only want to indicate 1 element we do not need the vector creation function c() because 1 is a vector in itself. With the rest we do something similar:

> m[c(2,3),c(1,2)]

     [,1] [,2]
[1,]    0    0
[2,]    0    0

> m[c(2,3),c(3,4)]

     [,1] [,2]
[1,]    1    1
[2,]    3    2

Another way to cut more advanced matrices is to cut through another matrix, first create a crop matrix:

> c <- matrix(c(1,1,2,2,3,3,4,4,3,3,4,4),3,4, byrow=TRUE)
> c

     [,1] [,2] [,3] [,4]
[1,]    1    1    2    2
[2,]    3    3    4    4
[3,]    3    3    4    4

When we see the matrix it is clear what we are going to cut, each number will indicate the new matrix that we hope to generate. And now we cut through split() , then apply lapply so that each clipping remains a nuance and not a vector.

> lapply(split(m, c), matrix, nr = 2)

$'1'
     [,1]
[1,]    1
[2,]    4

$'2'
     [,1]
[1,]    6
[2,]    7

$'3'
     [,1] [,2]
[1,]    0    0
[2,]    0    0

$'4'
     [,1] [,2]
[1,]    1    1
[2,]    3    2

As you can see, we end with a list of the matrices we originally wanted

    
answered by 20.10.2017 / 23:32
source