How to do matrix multiplication in R?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to do matrix multiplication in R?

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.

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

Relevant Projects

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

Churn Prediction in Telecom using Machine Learning in R
Estimating churners before they discontinue using a product or service is extremely important. In this ML project, you will develop a churn prediction model in telecom to predict customers who are most likely subject to churn.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

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.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.