Related Articles

Related Articles

Matlab – Matrix
  • Last Updated : 26 Nov, 2020

A Matrix is a two-dimensional array of elements. In MATLAB, the matrix is created by assigning the array elements that are delimited by spaces or commas and  using semicolons to mark the end of each row. Now let’s have a glance at some examples to understand it better.

Syntax:

a = [elements; elements]

Example: Creating a Matrix

MATLAB



filter_none

edit
close

play_arrow

link
brightness_4
code

% MATLAB program to create
% a matrix
  
%Number Matrix
x = [1 2 3;4 5 6;7 8 9]
  
%String Matrix
y = ['Geeks';'Geeks']

chevron_right


 Output:
 

 Example: Knowing the size of the Matrix 

MATLAB

filter_none

edit
close

play_arrow

link
brightness_4
code

% MATLAB program to know
% the size of a matrix
  
% Creating matrices
x = [1 2 3 4;4 5 6 7;7 8 9 10];
xSize = size(x)
  
y=['Geeks';'Geeks'];
ySize = size(y)

chevron_right


Output: 

Accessing the elements of a matrix

 To reference an element in a matrix, we write matrix(m, n). Here m ande n are row and column indexes. 

Example 1:



MATLAB

filter_none

edit
close

play_arrow

link
brightness_4
code

% MATLAB program to access
% a particular element
  
% Creating matrices
x = [1 2 3 4;4 5 6 7;7 8 9 10];
x(3,2)
  
y=['Geeks';'Geeks'];
y(1,2)

chevron_right


Output:

To access multiple elements in the matrix, we write 

matrix(x:y;,xy)

Example 2: 

MATLAB

filter_none

edit
close

play_arrow

link
brightness_4
code

% MATLAB program to access
% a multiple elements
  
% Creating matrices
x = [1 2 3 4;4 5 6 7;7 8 9 10];
  
% Accessing all rows and columns
x(:,:)
  
% Accessing all the elements
% in the first two rows
x(1:2,:)
  
% Accessing all the elements
% from 2nd to last element
% in every row.
x(:,2:end)
  
% Accessing elements from 2nd index
% to 3rd index in the first two columns
x(1:2,2:3)

chevron_right


Output: 

Adding elements/dimensions to a matrix 

To add elements/dimension to a matrix we can use one of the following methods: 



cat(dimension,A,B,….)

or

x = [A;B]

In the above syntax, A & B are matrices that we need to pass into the function cat() or concatenate by using square brackets. Here dimension parameter must be either 1 or 2 for table or timetable input. 

Example 1: 

MATLAB

filter_none

edit
close

play_arrow

link
brightness_4
code

% MATLAB program to add
% dimensions to a matrix
  
% Creating matrices
x = [1 2 3 4;4 5 6 7];
y = [7 8 9 10;11 12 13 14];
  
% Concatinating two matrices
% or adding a row
a = [x;y]
b = [x;11 12 13 14]
  
%Adding a column to a matrix
x(:,5)=[15 16]

chevron_right


Output: 

Example 2: 

MATLAB

filter_none

edit
close

play_arrow

link
brightness_4
code

% Matlab Program to 
% explain concatenation
  
x = [1 2 3 4;4 5 6 7];
  
% Creating a new matrix by
% concatinating multiple rows
% of a matrix
a = x([1,2,2,1],:)
    
% Creating a new matrix by
% concatenating multiple columns
% of a matrix
b = x(:,[1,3,2])

chevron_right


Output:

 Example 3: 

MATLAB

filter_none

edit
close

play_arrow

link
brightness_4
code

% MATLAB program to concatenate
% two matrices using cat() funtion
  
% Creating matrices
x = [1 2 3 4;4 5 6 7];
y = [1 2 3 4;4 5 6 7];
  
% Concatenating two matrices
% Using cat() function
a = cat(1,x,y)
b = cat(2,x,y)

chevron_right


Output: 

Example 4: Deleting a row/column of a matrix 

MATLAB

filter_none

edit
close

play_arrow

link
brightness_4
code

% MATLAB program to delete
% a row or a column
  
% Creating matrices
x = [1 2 3 4;4 5 6 7;7 8 9 10];
  
% Deleting 3rd row of
% a matrix
x(3,:) = []
  
% Deleting 3rd column in
% every row of a matrix
x(:,3) = []

chevron_right


Output:

Note: 

  • Unlike most of the programming languages, MATLAB array indexes start from 1.
  • To perform operations(almost all) on the matrices, dimensions must be the same.
  • A semicolon must be added at each end of the statement in order to avoid multiple outputs.
  • In MATLAB, a string is a character array.
  • In MATLAB, length() gives the length of an array, and size() gives the size of a matrix.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :