HOW DO I DO THAT IN MATLAB SERIES?

In this series, I am answering questions that students have asked me about MATLAB.

Contents

TOPIC

How do I conduct matrix operations in Matlab

SUMMARY

Language : Matlab 2008a; Authors : Sri Harsha Garapati, Daniel Miller, Autar Kaw; Mfile available at Last Revised : January 17, 2012; Abstract: This program shows you how to perform matrix operations i.e. addition, subtraction, multiplication, element to element multiplication

clc
clear all

INTRODUCTION

disp('ABSTRACT')
disp('   This program shows you how to perform matrix operations ')

disp(' ')
disp('AUTHOR')
disp('   Sri Harsha Garapati, Daniel Miller and')
disp('Autar K Kaw of http://autarkaw.wordpress.com')
disp(' ')
disp('MFILE SOURCE')
disp('   http://numericalmethods.eng.usf.edu/blog/matrix_ops_ex2_blog.m')
disp(' ')
disp('LAST REVISED')
disp('   January 17, 2012')
disp(' ')
ABSTRACT
   This program shows you how to perform matrix operations 
 
AUTHOR
   Sri Harsha Garapati, Daniel Miller and
Autar K Kaw of http://autarkaw.wordpress.com
 
MFILE SOURCE
   http://numericalmethods.eng.usf.edu/blog/matrix_ops_ex2_blog.m
 
LAST REVISED
   January 17, 2012
 

INPUTS

% Matrix, a
a=[1 2 6; 2 65 1; 4 2 1];
%Matix, b
b=[2 3 4; 2 1 45; 9 3 6];

DISPLAYING INPUTS

disp('INPUTS')
disp(' Matrix,a')
%printing matrix a
disp(a)
disp('Matrix, b')
%printing matrix b
disp(b)
INPUTS
 Matrix,a
     1     2     6
     2    65     1
     4     2     1

Matrix, b
     2     3     4
     2     1    45
     9     3     6

THE CODE

% a) adding two matrices
% [a]+[b]=[c]
c=a+b;

% b) subtracting two matrices
% [a]-[b]=[d]
d=a-b;

% c) muliplying two matrices
% [a]*[b]=[e]
e=a*b;

% d) two times the scalar product of matrix a and b is matrix f
f=2.*[a].*([b]);

DISPLAYING OUTPUTS

disp('  ')
disp('OUTPUTS')
disp(' a) Addition of two matrices a and b gives the resulting matrix:')
disp(c)
disp(' b) Subtraction of two matrices a and b gives the resulting matrix:')
disp(d)
disp(' c) Multiplication of two matrices a and b gives the resulting matrix:')
disp(e)
disp(' c) Two times the scalar product of two matrices a and b gives the resulting matrix:')
disp(f)
  
OUTPUTS
 a) Addition of two matrices a and b gives the resulting matrix:
     3     5    10
     4    66    46
    13     5     7

 b) Subtraction of two matrices a and b gives the resulting matrix:
    -1    -1     2
     0    64   -44
    -5    -1    -5

 c) Multiplication of two matrices a and b gives the resulting matrix:
          60          23         130
         143          74        2939
          21          17         112

 c) Two times the scalar product of two matrices a and b gives the resulting matrix:
     4    12    48
     8   130    90
    72    12    12