Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

MATLAB: Creating & Manipulating Matrices - Element Referencing, Sub-Matrices, Deletion, Study notes of Matlab skills

A comprehensive guide on creating matrices in matlab, referencing elements, creating sub-matrices, and deleting rows or columns. It covers the syntax for creating matrices, referencing individual elements, and creating sub-matrices using different methods. Additionally, it explains how to delete entire rows or columns by assigning an empty set of square brackets to that row or column.

What you will learn

  • How do you delete a row or column from a matrix in MATLAB?
  • How do you reference an element in a matrix in MATLAB?
  • How do you create a matrix in MATLAB?

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

weldon
weldon 🇺🇸

4.5

(10)

223 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MATLAB Matrix
A matrix is a two-dimensional array of numbers.
In MATLAB, you create a matrix by entering elements in each row as comma or space delimited numbers
and using semicolons to mark the end of each row.
For example, let us create a 4-by-5 matrix a
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]
MATLAB will execute the above statement and return the following result
a =
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
Referencing the Elements of a Matrix
To reference an element in the mth row and nth column, of a matrix mx, we write
mx(m, n);
For example, to refer to the element in the 2nd row and 5th column, of the matrix a, as created in the
last section, we type
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a(2,5)
MATLAB will execute the above statement and return the following result
ans = 6
To reference all the elements in the mth column we type A(:,m).
Let us create a column vector v, from the elements of the 4th row of the matrix a:
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
v = a(:,4)
MATLAB will execute the above statement and return the following result
pf3
pf4

Partial preview of the text

Download MATLAB: Creating & Manipulating Matrices - Element Referencing, Sub-Matrices, Deletion and more Study notes Matlab skills in PDF only on Docsity!

MATLAB – Matrix

A matrix is a two-dimensional array of numbers.

In MATLAB, you create a matrix by entering elements in each row as comma or space delimited numbers

and using semicolons to mark the end of each row.

For example, let us create a 4-by-5 matrix a

a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]

MATLAB will execute the above statement and return the following result –

a = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8

Referencing the Elements of a Matrix

To reference an element in the mth^ row and nth^ column, of a matrix mx , we write –

mx(m, n);

For example, to refer to the element in the 2nd row and 5th column, of the matrix a , as created in the

last section, we type –

a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]; a(2,5)

MATLAB will execute the above statement and return the following result –

ans = 6

To reference all the elements in the mth^ column we type A(:,m).

Let us create a column vector v, from the elements of the 4th^ row of the matrix a :

a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]; v = a(:,4)

MATLAB will execute the above statement and return the following result –

v = 4 5 6 7

You can also select the elements in the mth^ through nth^ columns, for this we write –

a(:,m:n)

Let us create a smaller matrix taking the elements from the second and third columns –

a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]; a(:, 2:3)

MATLAB will execute the above statement and return the following result –

ans = 2 3 3 4 4 5 5 6

In the same way, you can create a sub-matrix taking a sub-part of a matrix.

a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]; a(:, 2:3)

MATLAB will execute the above statement and return the following result –

ans = 2 3 3 4 4 5 5 6

In the same way, you can create a sub-matrix taking a sub-part of a matrix.

For example, let us create a sub-matrix say taking the inner subpart of a :

To do this, write –

Example

In this example, let us create a 3-by-3 matrix m, then we will copy the second and third rows of this

matrix twice to create a 4-by-3 matrix.

Create a script file with the following code –

a = [ 1 2 3 ; 4 5 6; 7 8 9]; new_mat = a([2,3,2,3],:)

When you run the file, it displays the following result –

new_mat = 4 5 6 7 8 9 4 5 6 7 8 9