R programming martix Exercises-part1

 

 

How to create an empty matrix in R ?

The term empty matrix has no rows and no columns. A matrix that contains missing values has at least one row and column, as does a matrix that contains zeros. In this article, we are going to see how to create an empty matrix in R Programming Language.

There are three ways of creating an empty matrix:

·         Using row and column.

·         Using only row.

·         Using only column.

Method 1: Using both row and column:

Here in this, we need to pass both row and column to create an empty matrix:

Syntax: matrix name = matrix(, nrow = value 1, ncol = value2)

Where:

·         Here matrix name can be any valid identifier

·         Value 1 is for number of rows.

·         Value 2 is for number of columns.

Example 1: In the below example, we created a mat variable, After creating mat variable we are using the matrix function to create a matrix and mentioning a number of rows and columns in it.

Below is the implementation:

# creating empty matrix,

# storing in variable mat and passing

# number of rows and columns

mat = matrix(, nrow = 1, ncol = 1)

 # printing empty matrix.

print(mat)

Here we got NA as output which means not a number or not available.

Example 2: 

# creating empty matrix,

# storing in variable mat1 and passing

@ number of rows and columns

mat1 = matrix(, nrow = 10, ncol = 10)

 # printing empty matrix.

print(mat1)

 

Method 2: Using only row :

Here we need to pass the only one row to create an empty matrix 

Syntax: matrix name = matrix(, nrow = value 1)

Where,

Here matrix name can be any valid identifier

value 1 is for number of rows.

Example 1: 

Mat<-matrix(,nrow=10)

 # printing empty matrix.

print(Mat)

 

Method 3: Using only column.

Here we need to pass the only columns to create an empty matrix.

Syntax: matrix name = matrix(, ncol = value 1)

Where,

Here matrix name can be any valid identifier

Value 1 is for number of column.

Below is the implementation:

Mat<-matrix(,ncol=10)

 #printing empty matrix.

print(Mat)

 

Fill an empty matrix in R

Syntax:

matrix_name[row,column]=value.

Where, row and column are the numbers in which the value is occupied.

Example:

# create empty matrix.

a<-matrix(nrow=4,ncol=4)

  # display matrix

print(a)

  # filling empty matrix with value at 

# 2nd row and 4 th column

a[2,4]=4

print(a)

  

 

# create empty matrix.

a<-matrix(nrow=4,ncol=4)

  

# filling empty matrix with value at 

# 4th column

a[,4]=4

print(a)

  

# create empty matrix.

a<-matrix(nrow=4,ncol=4)

  

# filling empty matrix with value at 

# 4th row

a[4,]=4

print(a)

 

We can replace with same value for all empty value in a matrix, by using [,].

Example

# create empty matrix.

a<-matrix(nrow=4,ncol=4)

  

# display matrix

print(a)

  

# replace empty matrix with 5 value

a[,]=5

print(a)

 

Example

 

# create empty matrix.

a<-matrix(nrow=4,ncol=4)

  

# display matrix

print(a)

  

# replace empty matrix with  

# 1 to 16 values

a[,]=1:16

print(a)

 

Element wise Matrix Multiplication in R

Approach

·         Create a matrix

·         Multiply two matrices

·         Verify the result.

Element-wise multiplication using “*” operator:

Syntax: matrix1*matrix*2…..matrix n

Example 1:

This code shows the element-wise multiplication of two matrices data1 and data2, Data comprises 5 rows and 2 columns:

# Creating matrices 10 elements each using

# range operator ":"

data1 <- matrix(1:10, nrow = 5) 

print(data1)

  

data2 <- matrix(11:20, nrow = 5) 

print(data2)

  

# perform element wise multiplication

print(data1*data2)

 

Example 2:

This code for multiplication of multiple matrices data1,data2,data3. All data comprises 5 rows created using the range operator.

# Creating matrices 10 elements each 

# using range operator ":"

data1 <- matrix(1:10, nrow = 5) 

print(data1)

  

data2 <- matrix(11:20, nrow = 5) 

print(data2)

  

data3 <- matrix(21:30, nrow = 5)

  

# perform element wise multiplication

print(data1*data2*data3)

Example 3:

This code shows the matrix is created using vectors. And matrix multiplication is done.

# vector a

a = c(3, 4, 5, 6, 7, 8)

  

# vector b

b=c(1, 3, 0, 7, 8, 5)

  

# Creating matrices using vector

data1 <- matrix(a, nrow = 3) 

print(data1)

  

data2 <- matrix(b, nrow = 3) 

print(data2)

  

print(data1*data2)

 

Example 4:

An example that shows multiplication column arrangement and matrices data1 and data2 and multiplied. Column wise we are going to perform matrix multiplication data1 and data2 comprises 3 columns and elements are created using vector.

# vector a

a = c(3, 4, 5, 6, 7, 8)

  

# vector b

b = c(1, 3, 0, 7, 8, 5)

  

# Creating matrices using vector

data1 <- matrix(a, ncol = 3) 

print(data1)

  

data2 <- matrix(b, ncol = 3) 

print(data2)

  

print(data1*data2)

Find the power of a matrix in R

Different ways of finding the power of matrix in R programming:

·         By using %^%.

·         By using a power function.

Method 1: By using %^%

Before using this we need to import expm library into our R studio.

Installing expm library into R studio:

Step 1: First you need to select tools.

Step 2: After selecting the tool you need to press install packages:

Below is the implementation:

We Import the expm and assigned values into the mat by using the matrix function. After that, we are finding the power of the matrix.

# loading expm library

library(expm) 

  

# creating mat variable and storing 

# matrix into it.

mat <- matrix(1:9, nrow=3)

  

# In matrix function data will fill column wise,

# here data will be 1 to 9 as we mentioned 1:9

# and rows will be 3 as we given nrows as 3

  

# finding power of matrix here power is 4

mat %^% 4

Method 2: By using a power function.

For using the power function we need to install matrixcalc package into our Rstudio

Below is the implementation:

Here we import the matrixcalc and assigned values into the mat by using matrix function. After that, we are finding the power of matrix by using power function.

# loading package

require(matrixcalc)

  

# creating matrix.

a<- matrix(1 : 9, nrow = 3)

  

# finding power of matrix by using power function.

# In matrix function data will fill column wise,

# here data will be 1 to 9 as we mentioned 1:9

# and rows will be 3 as we given nrows as 3

matrix.power(a, 9)

 

Raise a matrix to a fractional power in R

Matrices can be raised to integral as well as non-integral powers in R Programming Language. The computation of power of a matrix involves simultaneous multiplication of matrix as many times as the power integer is specified. However, in the case of fractional powers, we need to use the in-built R functions to simulate this operation. 

Method 1: Using expm package in R

The expm package can be used to perform various types of matrix computations, like logarithm, exponential, and others. This function computes the exponential of a square matrix, which is non-diagonalizable. The expm package includes logm and sqrtm methods, respectively. However, it cannot be used to work with negative fractional powers. The expm package can be installed in R directory using the following command : 

install.packages("expm)

The package is included in the script and then the expm method can be used which has the following syntax : 

expm(x), where x is the matrix

Code:

require(expm)

library("expm")

  

mat <- matrix(c(1, 1, 1, 1), 2, 2)

print ("Original Matrix")

print (mat)

modified_matrix <- 1.1*logm(mat)

  

# computing power matrix 

powmat <- expm(modified_matrix)

  

# printing the power matrix 

print ("Power Matrix")

print (powmat)

Method 2: Using powerplus package in R

Matrices can be raised to negative or positive non-integral powers using the powerplus package in R. It is used to raise a valid matrix to any power (even complex numbers are allowed). The matrices that can be used are either square matrices or diagonalisable matrices with positive eigenvalues. The powerplus package can be installed into the R directory by using the following command : 

install.packages("powerplus")

The package can then be included in the script and then the Matpow() method can be used, which can be used to exponential power computation of a square matrix.

Syntax: Matpow(M, power_number)

Arguments : 

·         M – A diagnosable square matrix

·         power_number – The non-integral power factor of the matrix

Returns :  A matrix raised to the power_number factor. 

Example 1: The following examples illustrate the Matpow() method in R.

# requiring the necessary package

require("powerplus")

  

# declaring a non diagonizable matrix

mat <- matrix(c(2, 0, 1, 1), ncol = 2) 

print ("Original Matrix")

print (mat)

  

# raising the matrix to a fractional 

# power 0.5

print ("Power Matrix")

Matpow(mat, 0.5)

Example 2: Negative integral powers.

Matpow() methods can be used to work out with negative integral powers also. The output is the negative matrix in case the result is odd, otherwise even. 

# requiring the necessary package

require("powerplus")

  

# declaring a non diagonizable matrix

mat <- matrix(c(2, 0, 1, 1), ncol = 2) 

print ("Original Matrix")

print (mat)

  

# raising the matrix to a fractional power 0.5

print ("Power Matrix")

Matpow(mat, -2.1)

 

Comments

Popular posts from this blog

How to create Animated 3d chart with R.

Linux/Unix Commands frequently used

R Programming Introduction