0% found this document useful (0 votes)
124 views9 pages

Creating Vectors and Matrices in MATLAB

Vectors are one-dimensional arrays, while matrices are two-dimensional arrays. Matlab counts elements from top to bottom and left to right. Common operations include creating, accessing, modifying, and performing calculations on vectors and matrices. Functions like zeros, ones, eye, and rand can generate matrices filled with specific values.

Uploaded by

Zack Obega
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views9 pages

Creating Vectors and Matrices in MATLAB

Vectors are one-dimensional arrays, while matrices are two-dimensional arrays. Matlab counts elements from top to bottom and left to right. Common operations include creating, accessing, modifying, and performing calculations on vectors and matrices. Functions like zeros, ones, eye, and rand can generate matrices filled with specific values.

Uploaded by

Zack Obega
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

VECTORS AND MATRICES

Which of the following is a vector and which one is a matrix

Definitions
Vectors and matrices: set of elements (array).

Matrix: two dimensional array.

Vector: one dimensional array.

Aren't they all matrices??

Creating vectors and matrices

Creating vectors and basic commands to deal with them

%Example - Write the following:


 
%%Row vector
row=[5 88 3 11] %Mind the spaces in between the values.
 

row=[5,88,3,11]% The spaces are replaced by comas

Each element has an assigned index:

If you want to refer to the element with the index No. 2

row(2)

Creating a subvector
sub1=row(2:4) %Sequential indexes

sub2=row([1 3 4]) %Non sequential indexes

Changing one value of a vector

 
sub1(2)=16

Expanding the vector

sub1(4)=22

sub1(6)=33 % It will fill the empty indexes with 0.

Creating a column vector

col=[11;22;33;44] % you need the semicolon

Vector of equally spaced elements

vec=1:5 % You can use the colon operator (:)

vec_step=1:2:10 %step=distance between the values.

vec_bigstep=1:3:5 %it will stop before the upper limit.

Decreasing vector

vec_dec=10:-2:1

%What happens if you write the following:


vec_dec=1:-2:10 %Empty vector, the lower limit is first.

Creating a column vector from a row vector: transpose

rowtrans = row'
Creating matrices and basic commands to deal with them

mat=[1 2 3;4 5 6] %the rows can be divided in semicolons.

Can you have different rows with different number of elements?

mat=[1 2 3
4 5 6] %The rows can be divided in lines.

Using the colon operator to create the rows

mat=[1:3;4:6]

Refering to the elements in a matrix (I need your help!)

mat(2,1) % Element in the row No. 2 and column 1.

mat(1:2,2:3) %Refering to a section of a matrix

mat(1,:) % Referng only to the first row

mat(:,3) %Refering only to the third column

Remember: Matlab counts from up to bottom and from left to right.

mat(1) %it will refer to the element of the first row, first column

mat(5) %it will refer to the element in the first row, column tree.

Redefining variables or elements in a matrix (I need your help!)

mat(2,2)=20 %it will change the element in (2,2)

mat(2,:)=[20 30 40] %it will change only the second row

Expanding the matrix

mat(4,:)=[20 30 40] % Adds the row No. 4 and fills the 3rd with zeros.
mat =
1 20 3
20 30 40
0 0 0
20 30 40

mat(:,5)= [20 30 40 50]' %Adds the row No. 4 and fills the 3rd with zeros.

mat =
1 20 3 0 20
20 30 40 0 30
0 0 0 0 40
20 30 40 0 50

Erasing rows and columns

mat(:,5)=[] % Eliminates column 5

mat =
1 20 3 0
20 30 40 0
0 0 0 0
20 30 40 0

mat([3,4],:)=[] %Eliminates rows 3 and 4

mat =
1 20 3 0
20 30 40 0

Calculations with matrices

%Create the following matrices


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

A =
1 2 3
4 5 6
7 8 9

B=A'

B =
1 4 7
2 5 8
3 6 9
Multiplication

B*A

ans =
66 78 90
78 93 108
90 108 126

Multiplication with vector

a=[4 9 1]

a =
4 9 1

a*B

ans =
25 67 109

B =
1 4 7
2 5 8
3 6 9

A.*B

ans =
1 8 21
8 25 48
21 48 81

Addition and substraction

A+B
A-B

Division

A./B
B./A %The order matters

A.\B %Elementwise but dividing the elements from B by the one from A

Potencies

A^2 % Normal multiplication of matrices

A.^2 %Elementwise multiplication

Some functions to use with matrices

Z=zeros(3,2) %Matrix of zeros with 3 rows and 2 columns.

Z =
0 0
0 0
0 0

z=zeros(3,1) %Column vector of zeros

z =
0
0
0

Ones=ones(5,4) %Matrix of ones

Ones =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

eye(4) %4x4 diagonal matrix

eye(2,4) %2x1 diagonal matrix

ans =
1 0 0 0
0 1 0 0

r=[1 2; 3 4]

r =
1 2
3 4

repmat(r,4,3) %Repeats the matrix (4 "rows" and 3 "columns")

ans =
1 2 1 2 1 2
3 4 3 4 3 4
1 2 1 2 1 2
3 4 3 4 3 4
1 2 1 2 1 2
3 4 3 4 3 4
1 2 1 2 1 2
3 4 3 4 3 4

rand(3,2) % random 3x2 matrix

ans =
0.8147 0.9134
0.9058 0.6324
0.1270 0.0975

randi(3,2) %matrix with values from 1 to 3 and of size 2x2.

randi([-5,5],4,5) %matrix with values from -5 to 5 and of size 4x5.

ans =
-2 -4 3 3 4
1 5 -4 5 5
5 5 -1 2 2
5 0 5 -5 3

linspace function: linspace(x,y,n) - vector of n elements within the range [x,y].


% Example
ls=linspace(3,15,5)

ls =
3 6 9 12 15

Hypermatrices:
One matrix of i x j dimensions and k "layers".

e.g. Grid Example master thesis.

% Example
AA(:,:,1)=[1 2 3; 4 5 6]
AA(:,:,2)=[2 3 4; 5 6 7]

%Example
AA(:,:,2)
 

You might also like