- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to multiply vector values in sequence with matrix columns in R?
To multiply vector values in sequence with matrix columns in R, we can follow the below steps −
First of all, create a matrix.
Then, create a vector.
After that, use t function for transpose and multiplication sign * to multiply vector values in sequence with matrix columns.
Example
Create the data frame
Let’s create a data frame as shown below −
M<-matrix(round(rnorm(75),2),ncol=3) M
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
[,1] [,2] [,3] [1,] 0.51 2.05 0.30 [2,] -0.74 0.18 -1.50 [3,] 0.63 -2.03 -1.16 [4,] 0.17 0.33 -0.68 [5,] 0.39 -1.87 0.06 [6,] -0.69 0.49 -0.54 [7,] -0.38 1.39 -2.19 [8,] -0.01 -1.14 -0.47 [9,] -1.19 -0.26 0.44 [10,] -0.80 -1.29 1.89 [11,] 1.31 0.00 0.30 [12,] 0.03 -0.92 0.90 [13,] 1.00 -1.04 -0.05 [14,] 0.82 -1.63 2.71 [15,] -0.89 0.41 -0.46 [16,] 0.19 2.30 0.62 [17,] -1.17 -0.36 0.23 [18,] 0.15 -0.05 0.04 [19,] 0.83 -0.02 0.30 [20,] 0.99 0.92 -0.09 [21,] -1.19 0.81 1.44 [22,] 0.79 -0.02 0.45 [23,] 1.51 -1.30 1.41 [24,] 0.73 0.32 -0.92 [25,] 0.98 -0.64 -1.33
Create the vector
Let’s create a vector as shown below −
V<-1:3 V
Output
[1] 1 2 3
Multiply vector values in sequence with matrix columns
Using t function for transpose and multiplication sign * to multiply V values in sequence with columns of matrix M as shown below −
M<-matrix(round(rnorm(75),2),ncol=3) V<-1:3 t(t(M)*V)
Output
[,1] [,2] [,3] [1,] 0.51 4.10 0.90 [2,] -0.74 0.36 -4.50 [3,] 0.63 -4.06 -3.48 [4,] 0.17 0.66 -2.04 [5,] 0.39 -3.74 0.18 [6,] -0.69 0.98 -1.62 [7,] -0.38 2.78 -6.57 [8,] -0.01 -2.28 -1.41 [9,] -1.19 -0.52 1.32 [10,] -0.80 -2.58 5.67 [11,] 1.31 0.00 0.90 [12,] 0.03 -1.84 2.70 [13,] 1.00 -2.08 -0.15 [14,] 0.82 -3.26 8.13 [15,] -0.89 0.82 -1.38 [16,] 0.19 4.60 1.86 [17,] -1.17 -0.72 0.69 [18,] 0.15 -0.10 0.12 [19,] 0.83 -0.04 0.90 [20,] 0.99 1.84 -0.27 [21,] -1.19 1.62 4.32 [22,] 0.79 -0.04 1.35 [23,] 1.51 -2.60 4.23 [24,] 0.73 0.64 -2.76 [25,] 0.98 -1.28 -3.99
- Related Articles
- How to multiply vector values in sequence with columns of a data frame in R?
- How to multiply vector values in sequence with columns of a data.table object in R?
- How to multiply a matrix with a vector in R?
- How to check matrix values equality with a vector values in R?
- How to multiply a matrix columns and rows with the same matrix rows and columns in R?
- How to multiply corresponding row values in a matrix with single row matrix in R?
- How to match the names of a vector in sequence with string vector values in another vector having same values in R?
- How to divide columns of a matrix by vector elements in R?
- How to replace values in a vector with values in the same vector in R?
- How to create vector in R with a sequence of numbers?
- How to extract vector using different index for columns in an R matrix?
- How to create a matrix using vector of string values in R?
- How to multiply each element of a larger vector with a smaller vector in R?
- How to multiple a matrix rows in R with a vector?
- How to create a vector with repeated values in R?

Advertisements