Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select entire matrix except certain rows and columns?

Tags:

r

matrix

I have a 5x4 matrix and I want to select all elements (to be set to 0) except those in rows 2 to 4 AND columns 2 to 3. Basically, all the elements along the "edges" of the matrix should be set to 0. Currently, my code is

mat[ -(2:4), -(2:3) ] <- 0

However, this (de)selects the elements in an OR fashion so instead, only the corners of the matrix are set to 0. How can I choose them in AND fashion?

like image 533
bunny Avatar asked Jan 12 '21 19:01

bunny


People also ask

How do I find a specific row in a matrix?

To get a specific row of a matrix, specify the row number followed by a comma, in square brackets, after the matrix variable name. This expression returns the required row as a vector.

How do I select different columns in Matlab?

Typically, it would be: xdatatemp =xdata(:,77:86) - to select columns 77 to 86.

How do you select a column in a matrix?

When we create a matrix in R, its column names are not defined but we can name them or might import a matrix that might have column names. If the column names are not defined then we simply use column numbers to extract the columns but if we have column names then we can select the column by name as well as its name.


3 Answers

Use functions row and col together with logical operations. The following works because R's matrices are in column-first order.

mat <- matrix(seq.int(5*4), nrow = 5)
mat[ !(row(mat) %in% 2:4) | !(col(mat) %in% 2:3) ] <- 0
mat
#     [,1] [,2] [,3] [,4]
#[1,]    0    0    0    0
#[2,]    0    7   12    0
#[3,]    0    8   13    0
#[4,]    0    9   14    0
#[5,]    0    0    0    0
like image 132
Rui Barradas Avatar answered Nov 02 '22 04:11

Rui Barradas


Another option with replace

replace(mat * 0, as.matrix(expand.grid(2:4, 2:3)), mat[2:4, 2:3])

-output

#      [,1] [,2] [,3] [,4]
#[1,]    0    0    0    0
#[2,]    0    7   12    0
#[3,]    0    8   13    0
#[4,]    0    9   14    0
#[5,]    0    0    0    0

data

mat <- matrix(seq.int(5*4), nrow = 5)
like image 23
akrun Avatar answered Nov 02 '22 02:11

akrun


Here is a two-step base R option

rs <- 2:4
cs <- 2:3
`<-`(mat[setdiff(seq(nrow(mat)), rs), ], 0)
`<-`(mat[, setdiff(seq(ncol(mat)), cs)], 0)

which gives

> mat
     [,1] [,2] [,3] [,4]
[1,]    0    0    0    0
[2,]    0    7   12    0
[3,]    0    8   13    0
[4,]    0    9   14    0
[5,]    0    0    0    0

Data

mat <- matrix(seq.int(5*4), nrow = 5)
like image 38
ThomasIsCoding Avatar answered Nov 02 '22 02:11

ThomasIsCoding