0% found this document useful (0 votes)
3 views

Matlab MatrixArray Guide

Matlab matrix guide

Uploaded by

afrah
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)
3 views

Matlab MatrixArray Guide

Matlab matrix guide

Uploaded by

afrah
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
You are on page 1/ 6

Creating Matrices/Arrays

%A matrix can be made in several ways

A = [ 1 2 3] %this is a 1x3 matrix/array/vector

A = 1×3
1 2 3

% To make matrices with several rows the ";" can be used to transition to
% next row

A2 = [ 1 2 ; 4 5 ] % creates a 2x2 matrix

A2 = 2×2
1 2
4 5

% Matrices can also be resized using "reshape". Written in the form


% newmatrix = reshape( old matrix, [r,c]) where [r,c] corresponds to the number of
% rows and columns of the dimesions of the desired new matrix. Consider A2

reshape( A2, [1,4]) % resized matrix must have same no. of entries as original

ans = 1×4
1 4 2 5

% row matrices can also be turned into column matrices using '. consider A5

A'

ans = 3×1
1
2
3

% the inverse of a matrix can be found using the inv(matrixname) operator

inv(A2) %can only be used if matrix is square

ans = 2×2
-1.6667 0.6667
1.3333 -0.3333

1
Matrix Operations

% values in matrices can be processed with single function or arthmetic


% operations

A+10

ans = 1×3
11 12 13

sin(A)

ans = 1×3
0.8415 0.9093 0.1411

% Two matrices can also be used in computation. Consider 3 matrices

M1 = [ 1 2 3 ; 4 5 6]

M1 = 2×3
1 2 3
4 5 6

M2 = [ 1 2 ; 3 4 ; 5 6]

M2 = 3×2
1 2
3 4
5 6

M3 = [ 6 7 8 ; 8 7 6]

M3 = 2×3
6 7 8
8 7 6

% regular matrix operations use regular operators, but they must have
% compatible dimensions M1/M3 and M2 are incompatible for addition and
% substration

M1 + M3

ans = 2×3

2
7 9 11
12 12 12

M1 - M3

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

% operations can also be done element wise by using . operators

M1.*M3

ans = 2×3
6 14 24
32 35 36

M1./M3

ans = 2×3
0.1667 0.2857 0.3750
0.5000 0.7143 1.0000

% matrices can also be squarerooted, and have exponents

sqrt(M1) % finds square root of each element

ans = 2×3
1.0000 1.4142 1.7321
2.0000 2.2361 2.4495

(M1).^2 % squares each element

ans = 2×3
1 4 9
16 25 36

Other Array Creation Methods

% additionally matrices can be made using specific key words


% Typically in the form keyword(r,c) where r and c are the desired number of
% rows and columns. Examples are highlighted below

zeros(3,2) %creates 2x2 matrix of all zeros


rand(3,3) %creates 3x3 matrix of randomly generated numbers
ones(2,2) %creates 1x2 matrix of all ones
% There are ways to create specific 1 row matrices/arrays(also called row vectors)
% using specialized operators.

3
% to create an array that has entries with a specific
% : is used, for example

1 : 2 : 10 % creates a 1x5 array starting with 1 with a difference of 2 between elements upto a

ans = 1×5
1 3 5 7 9

% general format is; starting value : interval distance : max value


% the operator linspace can also be used

linspace(2,0.5,4) % creates an array with 4 elements, starting value of 2 end value of 0.5

ans = 1×4
2.0000 1.5000 1.0000 0.5000

% general format of linspace operator;


% linspace(starting value, end value, number of entries)

Indexing and Obtaining values

%maximun and minimum values in a array can be found using min and
%max operators. Consider array V

V = [ 44 54 23 1 67]

V = 1×5
44 54 23 1 67

max(V)

ans = 67

min(V)

ans = 1

% There are two ways to index and array/matrix. Consider P

P = [ 1 2 3 ; 5 6 7 ; 9 10 11 ] % a 3x3 matrix

4
P = 3×3
1 2 3
5 6 7
9 10 11

% method 1; specifying row and column

P(3,2)

ans = 10

% method 2; using single subscript that goes down from first element of
% leftmost column called linear indexing

P(6)

ans = 10

% if you specify an element to be in a position outside the original matrix


% bounds, the matrix will resize to adjust itself.

P(4,4) = 16

P = 4×4
1 2 3 0
5 6 7 0
9 10 11 0
0 0 0 16

% to refer to multiple elements use a : to specify from which position.

P(1:3,2) % shows the first three elements in the second column

ans = 3×1
2
6
10

P(1,2:3) % shows the last two elements in row 1

ans = 1×2
2 3

% a : alone in the place of a row or column yields all the elements of that
% row/column, for example P(:,:) yeilds the original matrix.

5
6

You might also like