0% found this document useful (0 votes)
38 views3 pages

Experiment 1

This document provides an introduction to using MATLAB for matrix operations and linear algebra. It covers how to create vectors and matrices, generate random matrices, use the linspace function to create equally spaced vectors, and extract elements and subarrays from matrices. Key functions and operations discussed include creating row and column vectors, transposing matrices, generating random integer and real-valued matrices, and indexing/extracting portions of matrices.

Uploaded by

Madesh kumar
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)
38 views3 pages

Experiment 1

This document provides an introduction to using MATLAB for matrix operations and linear algebra. It covers how to create vectors and matrices, generate random matrices, use the linspace function to create equally spaced vectors, and extract elements and subarrays from matrices. Key functions and operations discussed include creating row and column vectors, transposing matrices, generating random integer and real-valued matrices, and indexing/extracting portions of matrices.

Uploaded by

Madesh kumar
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/ 3

MATLAB for the AY 2022-23 (Experiment 01)

Table of Contents
Matrix Theory............................................................................................................................................................1
Creating arrays (Row and column vectors)...........................................................................................................1
Creating Matrix..................................................................................................................................................... 2
Random Matrix Generation...................................................................................................................................2
linspace Function..................................................................................................................................................2
Extraction of elements/arrays............................................................................................................................... 2

Matrix Theory
Creating arrays (Row and column vectors)
% Creating arrays
A=[1 2 3 4 5]

A = 1×5
1 2 3 4 5

B=[1;2;3;4]

B = 4×1
1
2
3
4

C=A'

C = 5×1
1
2
3
4
5

D=transpose(C)

D = 5×1
1
2
3
4
5

% Equally spaced arrays


a=1:1:5

a = 1×5
1 2 3 4 5

b=0:0.5:3

b = 1×7
0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000

1
Creating Matrix
% Creating matrix
A=[1 2 3 4;5 6 7 8]

A = 2×4
1 2 3 4
5 6 7 8

B=[1:2:100;2:2:100]

B = 2×50
1 3 5 7 9 11 13 15 17 19 21 23 25
2 4 6 8 10 12 14 16 18 20 22 24 26

Random Matrix Generation


% random matrix
c=rand(3,4)

c = 3×4
0.3404 0.7513 0.6991 0.5472
0.5853 0.2551 0.8909 0.1386
0.2238 0.5060 0.9593 0.1493

% mat=randi(range,size) will give integer entry matrices


mat1=randi([1 5],[3 5])

mat1 = 3×5
2 5 2 4 5
5 2 1 3 3
2 5 2 2 3

mat2=randi(10,[3 5])

mat2 = 3×5
10 8 1 8 6
3 4 1 10 5
8 6 6 2 1

mat3=randi([-5 5],[3 5])

mat3 = 3×5
-2 -2 1 2 -5
-4 0 -3 3 -3
3 -4 2 -1 5

linspace Function
% linspace(start,end,no_of_points)
a=linspace(0,1,100);
b=linspace(0,2*pi,500);

Extraction of elements/arrays

2
% Consider the matrix A
A

A = 4×5
1 2 0 -1 4
2 5 -1 -1 2
3 10 -1 -1 2
4 0 0 5 2

A(1,5)=4 % Replaces the element in the 1st row and 5th column by 4

A = 4×5
1 2 0 -1 4
2 5 -1 -1 2
3 10 -1 -1 2
4 0 0 5 2

A2=A(:,2) % Extraction of 2nd column

A2 = 4×1
2
5
10
0

A3=A(:,3) % Extraction of 3rd column

A3 = 4×1
0
-1
-1
0

A([1 2 3],4) % Extraction of first three elements of the 4th column

ans = 3×1
-1
-1
-1

A(2,:) % Extraction of second row

ans = 1×5
2 5 -1 -1 2

A(end,:) % Extractin of last row

ans = 1×5
4 0 0 5 2

A(end,end-1)=5 %Think about this....:)

A = 4×5
1 2 0 -1 4
2 5 -1 -1 2
3 10 -1 -1 2
4 0 0 5 2

You might also like