You are on page 1of 10

University of

Engineering &Technology
Taxila

[CAED ASSIGNMENT]
[NO 1]

SUBMITTED BY:

SHEIKH ABUZAR AMJAD

REG NO:

19-CP-6

SUBMITTED TO:

DR ROMANA FARHAN

Prof. Romana Farhan| Computer Engineering And Applications | November 13, 2020
Q1: Define and operate with the vectors
V1 = [1 2 3 4 5 6 7 8 9 0]
V2 = [0.3 1.2 0.5 2.1 0.1 0.4 3.6 4.2 1.7 0.9]
V3 = [4 4 4 4 3 3 2 2 2 1]
a) Calculate, respectively, the sum of all the elements in vectors V1, V2,
and V3 .

OUTPUT

b) How to get the value of the fifth element of each vector? What happens
if we execute the command V1(0) and V1(11)? Remember if a vector has
N elements, their subscripts are from 1 to N.
Solution:
>> V1(5)
ans = 5
>> V2(5)
ans = 0.1000

PAGE 1
>> V3(5)
ans = 3
For What happens if we execute the command V1(0) and V1(11)?

c) Generate a new vector V4 from V2, which is composed of the first five
elements of V2.
Solution:
>> V4 = V2(1,1:5)
V4 =
0.3000 1.2000 0.5000 2.1000 0.1000
d) Generate a new vector V5 from V2, which is composed of the last five
elements of V2.
Solution:
>> V5 = V2(1,6:10)
V5 =
0.4000 3.6000 4.2000 1.7000 0.9000

e) Derive a new vector V6 from V2, with its 6th element omitted.

f) Derive a new vector V7 from V2, with its 7th element changed to 1.4.

g) Derive a new vector V8 from V2, whose elements are the 1st, 3rd,
5th, 7th, and 9th elements of V2.
Solution:
>> V6 = [V2(1,1:5) V2(1,7:10)]

V6 =

0.3000 1.2000 0.5000 2.1000 0.1000 3.6000 4.2000 1.7000 0.9000

>> V7 =[V2(1,1:6) 1.4 V2(1,8:10)]

V7 =

0.3000 1.2000 0.5000 2.1000 0.1000 0.4000 1.4000 4.2000 1.7000


0.9000

PAGE 2
>> V8 = [V2(1,1:2:9)]

V8 =

0.3000 0.5000 0.1000 3.6000 1.7000

h) What are the results of

 9-V1

 V1*5

 V1+V2

 V1-V3

 V1.*V2

 V1*V2

 V1.^2

 V1.^V3

 V1^V3

 V1 == V3

 V1>6

 V1>V3

 V3-(V1>2)

 (V1>2) & (V12) | (V1)


>> 9-V1

ans =

8 7 6 5 4 3 2 1 0 9

>> V1*5

ans =

5 10 15 20 25 30 35 40 45 0

>> V1+V2

PAGE 3
ans =

1.3000 3.2000 3.5000 6.1000 5.1000 6.4000 10.6000 12.2000 10.7000


0.9000

>> V1-V3

ans =

-3 -2 -1 0 2 3 5 6 7 -1

>> V1.*V2

ans =

0.3000 2.4000 1.5000 8.4000 0.5000 2.4000 25.2000 33.6000 15.3000


0

>> V1*V2

Error using *

Incorrect dimensions for matrix multiplication. Check that the number of columns in the
first matrix matches the number of rows in the

second matrix. To perform elementwise multiplication, use '.*'.

>> V1.^2

ans =

1 4 9 16 25 36 49 64 81 0

>> V1.^V3

ans =

1 16 81 256 125 216 49 64 81 0

>> V1^V3

Error using ^ (line 51)

Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and
the power is a scalar. To perform elementwise

matrix powers, use '.^'.

>> V1 == V3

ans =

1×10 logical array

0 0 0 1 0 0 0 0 0 0

PAGE 4
>> V1>6

ans =

1×10 logical array

0 0 0 0 0 0 1 1 1 0

>> V1>V3

ans =

1×10 logical array

0 0 0 0 1 1 1 1 1 0

>> V3-(V1>2)

ans =

4 4 3 3 2 2 1 1 1 1

>> (V1>2) & (V1<6)

ans =

1×10 logical array

0 0 1 1 1 0 0 0 0 0

>> (V1>2) | (V1<6)

ans =

1×10 logical array

1 1 1 1 1 1 1 1 1 1

PAGE 5
Q2) Extract a 4x4 matrix from the 6x6 matrix and 4x1 matrix from 6x1 .
Solution:

clear all;
clc;
A = randi([0, 20],[6, 6])
B = A(1:4,1:4)
C = randi([0, 10],[6, 1])
D = C(1:4,:)

OUTPUT

A 6×6 matrix

A 4×4 matrix obtained from a 6×6 matrix.

A 6×1 matrix

A 4×1 matrix obtained from 6×1 matrix

PAGE 6
Q3) Find the inverse of a 2x2 matrix using MATLAB.
Solution:

CODE

Q4) Generate a 4x5 zero’s matrix and 4x5 one’s matrix and add
them.
Solution: OUTPUT
clear all;
clc;

O = zeros(4,5)
I = ones(4,5)
x = O+I

PAGE 7
Q5) Suppose if a= 𝟏 𝟐 𝟑
𝟒 𝟓 𝟔
𝟕 𝟖 𝟗
Then using zero’s matrix command, write a command(s) so that the
output becomes:

a= 1 2 3
4 5 6
7 8 9
0 0 0
0 0 0
Solution:

PAGE 8
Q6) Suppose if a= 1 2 3
4 5 6
7 8 9
Then using zero’s matrix command, write a command(s) so that the
output becomes:

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

Solution:

The END
PAGE 9

You might also like