Você está na página 1de 11

Q.1. Create a symmetric matrix m with five rows and five columns.

Calculate its transpose


and verify that the matrix is symmetric by observing that m=m.
ANSWER:
>> pascal(5)

ans =

6 10 15

4 10 20 35

5 15 35 70

>> ans'

ans =

6 10 15

4 10 20 35

5 15 35 70

Q.2. Create a random matrix with 3 rows and 4 columns. The matrix
values should be integer
b/w (0100), than change its any 2 rows values by 1.5.
clc
clear all;

close all;
x=rand(3,4)*100
x(1,:)=1.5
x(2,:)=1.5

x=
95.7167 14.1886 79.2207 3.5712
48.5376 42.1761 95.9492 84.9129
80.0280 91.5736 65.5741 93.3993
x=

1.5000 1.5000 1.5000 1.5000


48.5376 42.1761 95.9492 84.9129
80.0280 91.5736 65.5741 93.3993
x=
1.5000 1.5000 1.5000 1.5000
1.5000 1.5000 1.5000 1.5000
80.0280 91.5736 65.5741 93.3993

Q.3. 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]

Calculate, respectively, the sum of all the elements in vectors V1, V2,
and V3
ANSWER:
clc
clear all;
close all;
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];

y=sum(V1)
z=sum(V2)
S=sum(V3)
ANSWER:
y =
45
z =
15.0000
S =
29

b) How to get the value of the fifth element of


each vector?
ANSWER:
clc
clear all;
close all;
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=V1(5)
B=V2(5)
C=V3(5)
A =
5
B =
0.1000
C =
3

What happens if we execute the command V1(0) and


V1(11)?
ANSWER:
clc
clear all;
close all;
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=V1(0)
C=V1(11)
That give an error due not present an 11 digit in vector

Remember if a vector has N elements, their subscripts


are from 1 to N

c) Generate a new vector V4 from V2, which is


composed of the first five elements of V2.
ANSWER:
clc
clear all;
close all;
V1 = [1 2 3 4 5 6 7 8
V2 = [0.3 1.2 0.5 2.1
V3 = [4 4 4 4 3 3 2 2
V4=[V2(1) V2(2) V2(3)

9 0];
0.1 0.4 3.6 4.2 1.7 0.9];
2 1];
V2(4) V2(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
ANSWER:
clc
clear all;
close all;
V1 = [1 2 3 4 5 6 7 8
V2 = [0.3 1.2 0.5 2.1
V3 = [4 4 4 4 3 3 2 2
V5=[V2(6) V2(7) V2(8)

9 0];
0.1 0.4 3.6 4.2 1.7 0.9];
2 1];
V2(9) V2(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.
ANSWER:
clc
clear all;
close all;

V1 = [1 2
V2 = [0.3
V3 = [4 4
V6=[V2(1)

3 4 5 6 7 8
1.2 0.5 2.1
4 4 3 3 2 2
V2(2) V2(3)

9 0];
0.1 0.4 3.6 4.2 1.7 0.9];
2 1];
V2(4) V2(5) V2(7) V2(8) V2(9) V2(10)]

V6 =
0.3000
1.2000
0.5000
4.2000
1.7000
0.9000

2.1000

0.1000

3.6000

f) Derive a new vector V7 from V2, with its 7th


element changed to 1.4.
ANSWER:
clc
clear all;
close all;
V1 = [1 2 3 4 5 6 7 8
V2 = [0.3 1.2 0.5 2.1
V3 = [4 4 4 4 3 3 2 2
V7=[V2(1) V2(2) V2(3)

9 0];
0.1 0.4 3.6 4.2 1.7 0.9];
2 1];
V2(4) V2(5) V2(6) 1.4 V2(8) V2(9) V2(10)]

V7 =
0.3000
1.2000
0.5000
2.1000
1.4000
4.2000
1.7000
0.9000

0.1000

0.4000

g) Derive a new vector V8 from V2, whose


elements are the 1st, 3rd, 5th, 7th, and 9th
elements of V2
ANSWER:
clc
clear all;
close all;
V1 = [1 2 3 4 5 6
V2 = [0.3 1.2 0.5
V3 = [4 4 4 4 3 3
V8=[V2(1) V2(3)

7 8 9 0];
2.1 0.1 0.4 3.6 4.2 1.7 0.9];
2 2 2 1];
V2(5) V2(7) V2(9)]

V8 =
0.3000

0.5000

0.1000

3.6000

h) What are the results of


9-V1clc
clear all;
close all;
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];
V5=9-V1

1.7000

V5 =
8

V1*5
ANSWER:
clc
clear all;
close all;
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];
V4=V1*5

V4 =
5 10 15 20 25 30 35 40 45

V1+V2
ANSWER:
clc
clear all;
close all;
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];
V7=V1+V2

Columns 1 through 8
1.3000 3.2000 3.5000 6.1000 5.1000 6.4000 10.6000 12.2000
Columns 9 through 10
10.7000 0.9000

V1-V3
ANSWER:
clc
clear all;
close all;
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];
V7=V1-V3

V7 =
-3 -2 -1

V1.*V2

7 -1

ANSWER:
clc
clear all;
close all;
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];
V7=V1.*V2

V7 =
Columns 1 through 8
0.3000 2.4000 1.5000 8.4000 0.5000 2.4000 25.2000 33.6000
Columns 9 through 10
15.3000

V1*V2
ANSWER:
It give an error

V1*V2
Answer:
clc
clear all;
close all;
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];
V=V1.^2

V=
1

9 16 25 36 49 64 81

V1.^V3
Answer:
clc
clear all;
close all;
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];
V=V1.^V3

V=

1 16 81 256 125 216 49 64 81

V1^V3
Answer:
clc
clear all;
close all;
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];
V=V1^V3

There is an error

V1 == V3
Answer:
clc
clear all;
close all;
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];
V=V1 == V3

V=
0

V1>6
Answer:
clc
clear all;
close all;
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];
V=V1>6

V=
0

V1>V3
Answer:
clc
clear all;
close all;
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];

V=V1>V3

V=
0

V3-(V1>2)
Answer:
clc
clear all;
close all;
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];
V=V3-(V1>2)

V=
4

(V1>2) & (V1<6)


Answer:
clc
clear all;
close all;
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];
V=(V1>2) & (V1<6)

V=
0

(V1>2) | (V1<6)
Answer:
clc
clear all;
close all;
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];
V=(V1>2) | (V1<6)

V=

any(V1)
ANSWER:
clc
clear all;
close all;
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];
V=any(V1)

V=
1

all(V1)
ANSWER:
clc
clear all;
close all;
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];
V=all(V1)

V=
0

2. Compare a script and a function


z = myfunction(x,y)
z=
82.2871 92.2510 28.1283
91.4850 63.8683 55.2350
12.8257 9.8516 96.7082
a = 1:10
a=
1

9 10

b = 2:11
b=
2

9 10 11

myfunction(a,b)
ans =
3

9 11 13 15 17 19 21

Você também pode gostar