How to do matrix multiplication in R?

This recipe helps you do matrix multiplication in R

Recipe Objective

How to do matrix multiplication in R? A matrix is a two-dimensional data structure i.e. a matrix contains rows and columns. Matrices are used for performing mathematical calculations.. Matrix multiplication produces a single matrix by multiplying two different given matrices. The recipe provides an example of matrix multiplication.

Explore Interesting IoT Project Ideas for Practice

Step 1 - Create a matrix

Syntax- A matrix function takes the following input values : matrix(data,nrow,ncol,byrow,dimnames). data - is our input matrix value, nrow and ncol - are the number of rows and columns required, if byrow=TRUE , the input numbers are arranged by rows and if byrow=FALSE, then they are arranged by columns. dimnames - assigns names to rows and columns of a data frame.

m1 <- matrix(c(1:8), nrow = 4, ncol = 4, byrow = TRUE) print(m1)

"m1 matrix":
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    1    2    3    4
[4,]    5    6    7    8

m2 <- matrix(c(11:18), nrow = 4, ncol = 4, byrow = TRUE) print(m2)

"m2 matrix":
     [,1] [,2] [,3] [,4]
[1,]   11   12   13   14
[2,]   15   16   17   18
[3,]   11   12   13   14
[4,]   15   16   17   18

Step 2 - Matrix Multiplication

print(m1*m2) # element wise multiplication

"Output of the code is ":
     [,1] [,2] [,3] [,4]
[1,]   11   24   39   56
[2,]   75   96  119  144
[3,]   11   24   39   56
[4,]   75   96  119  144

print(m1%*%m2) # inner product of the two

"Output of the code is ":

print(m1%*%m2)        # inner product of the two matrices
     [,1] [,2] [,3] [,4]
[1,]  134  144  154  164
[2,]  342  368  394  420
[3,]  134  144  154  164
[4,]  342  368  394  420

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

MLOps using Azure Devops to Deploy a Classification Model
In this MLOps Azure project, you will learn how to deploy a classification machine learning model to predict the customer's license status on Azure through scalable CI/CD ML pipelines.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks