0% found this document useful (0 votes)
17 views4 pages

MATLAB Basic Matrix Operations Guide

The document outlines basic matrix operations in MATLAB, including addition, subtraction, multiplication, division, and finding the inverse of a matrix. It provides detailed MATLAB programs for each operation, along with conditions for valid operations based on matrix dimensions. Additionally, it includes a step-by-step procedure for running the programs in MATLAB.

Uploaded by

Divyashree
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)
17 views4 pages

MATLAB Basic Matrix Operations Guide

The document outlines basic matrix operations in MATLAB, including addition, subtraction, multiplication, division, and finding the inverse of a matrix. It provides detailed MATLAB programs for each operation, along with conditions for valid operations based on matrix dimensions. Additionally, it includes a step-by-step procedure for running the programs in MATLAB.

Uploaded by

Divyashree
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

BASIC MATRIX OPERATIONS

AIM:- To write matlab programs for basic matrix operation.


OBJECTIVES:-
Upon completion of this experiment the students will be able to
1. To identify basic mathematical operators in MATLAB for matrix
manipulation
Operators
Arithmetic operations
+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponentiation
To write a matrix product as C=A*B where A is an m x n matrix and B is an n x k
matrix. MATLAB allows to be carried out on matrices in straightforward ways as long as the
operation makes sense mathematically and the operands are compatible. Thus,
A+B or A-B is valid if A and B are of the same size,
A*B is valid if A's number of columns equals B's number of rows,
A/B is valid and equals A · B -1 for same-size square matrices, and
A^2 which equals A*A, makes sense only if A is square.
Array operation:
·* element-by-element multiplication
./ element-by-element left division
.\ element-by-element right. Division
.^ elernerrt-by-element exponentiation
.’ nonconjugated transpose
Examples:
If U = [u1 u2 u3…….] and V = [v1 v2 v3 …………….] then
U . *V produces [u1v1 u2v2 u3v3 . . . ]
U . /V produces [ u1/v1 u2/v2 u3/ v3 ... ]
U . ^V produces [ u1v1 u2v2 u3v3 …..]

PROGRAM
Program 1
%Matrix addition
a = input('\nEnter the first matrix = ');
b = input('\nEnter the second matrix = ');

[ma na] = size(a);


[mb nb] = size(b);

if(ma==mb&&na==nb)

Page 1 of 4
HDL and MATLAB at GPC Thrikaripur by SITTTR Kerala
Basic Matrix Operations

c =a+b;
disp(' The sum of two matrices is')
disp(c);
else
disp(' Matrix dimensions Must agree for addition');
end
Program 2
%Matrix Subtraction
a = input('\nEnter the first matrix = ');
b = input('\nEnter the second matrix = ');

[ma na] = size(a);


[mb nb] = size(b);

if(ma==mb&&na==nb)

c =a-b;
disp(' The Difference of two matrices is')
disp(c);
else
disp(' Matrix dimensions Must agree for subtraction');
end

Program 3
%matrix multiplication
a = input('\nEnter the first matrix = ');
b = input('\nEnter the second matrix = ');
[ma na] = size(a);
[mb nb] = size(b);

w= input('Enter \n 1 for Array Multiplication\n 2 for Matrix multiplication\n =');


if(w==1)
if(ma==mb&&na==nb)

c =a.*b;
disp(' The Element wise product of two matrices is')
disp(c);
else
disp(' Matrix dimensions Must agree for array multiplication');
end
elseif(w==2)
if(na==mb)
c =a*b;
disp(' The Matrix Product of two matrices is')
Page 2 of 4
HDL and MATLAB at GPC Thrikaripur by SITTTR Kerala
Basic Matrix Operations

disp(c);
else
disp(' [Link] columns of 1st matrix must be equal to [Link] rows of 2nd matrix for
matrix multiplication');
end
else
disp(' You have not selected any operation');
end

Program 4
%Matrix Division
clc
clear all;
a = input('\nEnter the first matrix = ');
b = input('\nEnter the second matrix = ');
[ma na] = size(a);
[mb nb] = size(b);

if(ma==mb&&na==nb)
c =a./b;
disp(' The Element wise quotient two matrices is')
disp(c);
else
disp(' Matrix dimensions Must agree for array Division');
end

Program 5
%inverse of a matrix
a = input('\nEnter the matrix = ');
[ma na] = size(a);
if(na~=ma)
disp('The matrix is not square');
elseif(det(a)==0)
disp('The matrix is singular');
else
c=inv(a);
disp(' The Result is')
disp(c);
end

PROCEDURE
1. Open MATLAB software
2. Select Desktop  Desktop layout Default; to get all relevant sub-windows on a single
window

Page 3 of 4
HDL and MATLAB at GPC Thrikaripur by SITTTR Kerala
Basic Matrix Operations

3. Now type the following on command window


clc
A = [1 2 3 ; 4 5 -1 ; 7 4 2]
B = [1 4 1 ; 3 2 9; 3 -5 1]
C=A+B
D=A-B
S = inv(A)
T = inv(B)
E = A .* B
F=A*B
N = A ./B
J = A/B
K = A*(inv(B))
L=A\B
M = inv(A) * B
4. Now create a script file( .m file) by selecting File  New Blank M-file
5. Type the program1 and save it as basic_op.m
(Note: File Name should be different from any variable names, in built functions, and do
not use any special character other than underscore . The first character of the name of a
file or variable should be an alphabet)
6. Now the program can be run by either pressing F5 Key or selecting
Run command from Debug menu or by simply pressing the play button () on the debug
toolbar
7. On command window Enter the input matrices and press Enter
8. Observe and note down the Result
9. Repeat Steps 4 to 8 for other programs

Page 4 of 4
HDL and MATLAB at GPC Thrikaripur by SITTTR Kerala

Common questions

Powered by AI

MATLAB mitigates user errors by validating inputs against mathematical compatibility requirements before operations. For instance, it checks matrix dimensions for addition, subtraction, and element-wise operations, and it verifies rows and columns compatibility for standard multiplication. If conditions are unmet, MATLAB provides explanatory error messages, guiding users to correct input mistakes, thus reducing execution errors related to dimension mismatches .

MATLAB can perform element-wise division of matrices using the './' operator. For element-wise division to be valid, the matrices must be of the same size, meaning they have identical dimensions in terms of rows and columns. If the dimensions do not match, MATLAB will prompt that 'Matrix dimensions must agree for array Division' .

In MATLAB, array multiplication is conducted using the '.*' operator, performing element-wise multiplication where individual elements of two matrices are multiplied together. It requires the matrices to be the same size. Matrix multiplication uses the '*' operator, requiring the number of columns in the first matrix to equal the number of rows in the second matrix. MATLAB checks these conditions before performing the operations to ensure mathematical validity. If the conditions are not met, an error message provides guidance on the correct requirements .

Function naming conventions in MATLAB script files are crucial to avoid conflicts with existing functions and ensure readability. The name should start with an alphabet and should not match any variables or built-in function names, ensuring clarity and preventing errors during execution. Proper naming results in better organization and understanding of the script's purpose, which is especially important in complex matrix operations .

Matrix addition in MATLAB is only valid if the two matrices involved are of the same size. This means that both matrices must have the same number of rows and columns. If this condition is not met, MATLAB will display an error message stating that 'Matrix dimensions must agree for addition' .

The inverse of a matrix can be calculated in MATLAB only if the matrix is square (same number of rows and columns) and non-singular (its determinant is not zero). If either of these conditions is not met, MATLAB will either indicate that 'The matrix is not square' or 'The matrix is singular' .

The use of a script file (.m file) in MATLAB is significant as it allows users to write, save, and execute multiple commands for matrix operations efficiently. It ensures that the code can be reused and modified without retyping or losing changes, thereby facilitating debugging and consistent execution of programs for tasks such as matrix addition, subtraction, multiplication, and inversion .

The recommended procedures for executing matrix operations in MATLAB include opening MATLAB software, setting the desktop layout to default, entering commands in the command window, creating and saving script files (.m files), and running programs using the appropriate methods. This structured approach ensures the user can input, execute, and view results efficiently and effectively .

Control structures, such as if-else statements, facilitate decision-making in MATLAB by allowing programs to execute specific code blocks based on conditions. In matrix operations, these structures ensure that operations such as addition, subtraction, or division are only performed when the matrices satisfy certain compatibility criteria, like matching dimensions. This reduces errors and makes the software capable of handling various scenarios with structured logic .

Matrix compatibility in terms of dimensions governs which operations can be performed in MATLAB. Operations like addition, subtraction, and element-wise multiplication require matrices to have the same dimensions, while matrix multiplication requires the first matrix's columns to match the second matrix's rows. Should these constraints not be met, MATLAB will not execute the operations, and instead, display error messages that state the necessary conditions, thus ensuring users adhere to mathematical rules for matrix operations .

You might also like