Computer Science, asked by vsanil2242, 1 year ago

Which R command creates a 2 by 2 matrix with the values 1,2,3 and 4?

Answers

Answered by paulaiskander2
4

R has no direct way to create an arbitrary matrix. You have to first list all the entries of the matrix as a single vector (an m by n matrix will need a vector of length mn) and then fold the vector into a matrix.

To create this matrix: \left[\begin{array}{ccc}1&2\\3&4\end{array}\right]

So first, we list the entries column by column to get the matrix, as follows:

A = matrix(c(1,3,2,4),nrow=2)

The nrow=2 command tells R that the matrix has 2 rows (then R can compute the number of columns by dividing the length of the vector by nrow.)


Answered by Sidyandex
1

R has no direct way to create an arbitrary matrix.

You have to first list all the entries of the matrix as a single vector (an m by n matrix will need a vector of length mn) and then fold the vector into a matrix.


To create this matrix:


So first, we list the entries column by column to get the matrix, as follows:


A = matrix(c(1,3,2,4),nrow=2)


The nrow=2 command tells R that the matrix has 2 rows (then R can compute the number of columns by dividing the length of the vector by nrow.)

Similar questions