0% found this document useful (0 votes)
31 views3 pages

DS Matrix QP

The document contains 7 questions about performing operations on vectors and matrices in R. It includes creating vectors with specified values or using sequences, sorting vectors in ascending and descending order, creating blank and filled matrices with defined row and column names, accessing elements of matrices, and performing basic mathematical operations on matrices including addition, subtraction, multiplication and division. Sample output is provided for each question.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views3 pages

DS Matrix QP

The document contains 7 questions about performing operations on vectors and matrices in R. It includes creating vectors with specified values or using sequences, sorting vectors in ascending and descending order, creating blank and filled matrices with defined row and column names, accessing elements of matrices, and performing basic mathematical operations on matrices including addition, subtraction, multiplication and division. Sample output is provided for each question.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Write a R program to :

(i) Create a vector ‘x’ having series of values from 1 to 15 and print it.

(ii) Create a vector with sequence starting from 1 to 3 with a increment


value

of 0.3 using seq() function.

OUTPUT:
[1] "New vector using : operator-"
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[1] "New vector using seq() function-"
[1] "Specify step size:"
[1] 1.0 1.3 1.6 1.9 2.2 2.5 2.8

2. Write a R program to sort a Vector in ascending and descending order.

Output:
[1] "Original Vectors:"
[1] 10 20 30 25 9 26
[1] "Sort in ascending order:"
[1] 9 10 20 25 26 30
[1] "Sort in descending order:"
[1] 30 26 25 20 10 9

3. Write a R program to create a blank matrix with 3 rows and 5 columns.

Output:
[,1] [,2] [,3] [,4] [,5]
[1,] NA NA NA NA NA
[2,] NA NA NA NA NA
[3,] NA NA NA NA NA

4. Write a R program to create a matrix taking a vector of numbers


(values starting from 1 to 16 is order) as input. Create the matrix in Row
order. Display the matrix.

Output:
[1] "Original Matrix:"
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16

5. Write a R program to create a matrix with 4 rows and 4 columns taking


a given vector of numbers (values from 1 to 16) as input and define the
column and row names. Row names can be ‘Num1’, ‘Num2’ … etc. and
column names can be Val1, Val2,.. etc. andDisplay the matrix.

Output:

Val1 Val2 Val3 Val4


Num1 1 2 3 4
Num2 5 6 7 8
Num3 9 10 11 12
Num4 13 14 15 16

6. For the matrix created in the above question, write a R program to


access the element at 3rd column and 2nd row, only the 3rd row and only
the 4th column of a given matrix.

Output:

[1] "Access the element at 3rd column and 2nd row:"


[1] 7

[1] "Access only the 3rd row:"


Val1 Val2 Val3 Val4
9 10 11 12
[1] "Access only the 4th column:"
Num1 Num2 Num3 Num4
4 8 12 16
7. Write a R program to create two 2x3 matrix and add, subtract, multiply
and divide the matrixes.

Output:

[1] "Matrix-1:"
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[1] "Matrix-2:"
[,1] [,2] [,3]
[1,] 0 2 0
[2,] 1 3 2
[1] "Result of addition"
[,1] [,2] [,3]
[1,] 1 5 5
[2,] 3 7 8
[1] "Result of subtraction"
[,1] [,2] [,3]
[1,] 1 1 5
[2,] 1 1 4
[1] "Result of multiplication"
[,1] [,2] [,3]
[1,] 0 6 0
[2,] 2 12 12
[1] "Result of division:"
[,1] [,2] [,3]
[1,] Inf 1.500000 Inf
[2,] 2 1.333333 3

You might also like