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

Creating Vectors (A)

The document demonstrates creating and operating on vectors in MATLAB. It defines several vectors, performs operations on them such as addition and indexing, and uses flow control structures like for loops. A key difference between scripts and functions is explained, where scripts run all code directly and functions require arguments to be passed in and return outputs.

Uploaded by

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

Creating Vectors (A)

The document demonstrates creating and operating on vectors in MATLAB. It defines several vectors, performs operations on them such as addition and indexing, and uses flow control structures like for loops. A key difference between scripts and functions is explained, where scripts run all code directly and functions require arguments to be passed in and return outputs.

Uploaded by

Boris Monreal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

Creating Vectors

(a)

A = [1 0 4 5 3 9 0 2]
a = [4 5 0 2 0 0 7 1]

A =

1 0 4 5 3 9 0 2

a =

4 5 0 2 0 0 7 1

(b)

B = [A a]
C = [a,A]

B =

Columns 1 through 13

1 0 4 5 3 9 0 2 4 5 0 2 0

Columns 14 through 16

0 7 1

C =

Columns 1 through 13

4 5 0 2 0 0 7 1 1 0 4 5 3

Columns 14 through 16

9 0 2

(c)

D = zeros (1,50)
E = ones (1,100)

D =

Columns 1 through 13

0 0 0 0 0 0 0 0 0 0 0 0 0

Columns 14 through 26
0 0 0 0 0 0 0 0 0 0 0 0 0

Columns 27 through 39

0 0 0 0 0 0 0 0 0 0 0 0 0

Columns 40 through 50

0 0 0 0 0 0 0 0 0 0 0

E =

Columns 1 through 13

1 1 1 1 1 1 1 1 1 1 1 1 1

Columns 14 through 26

1 1 1 1 1 1 1 1 1 1 1 1 1

Columns 27 through 39

1 1 1 1 1 1 1 1 1 1 1 1 1

Columns 40 through 52

1 1 1 1 1 1 1 1 1 1 1 1 1

Columns 53 through 65

1 1 1 1 1 1 1 1 1 1 1 1 1

Columns 66 through 78

1 1 1 1 1 1 1 1 1 1 1 1 1

Columns 79 through 91

1 1 1 1 1 1 1 1 1 1 1 1 1

Columns 92 through 100

1 1 1 1 1 1 1 1 1

(d)

F = [1 2 3 4: 30]
G = [25:-3:1]
H = [0 0.2 0.4 0.6: 2.0]

F =

Columns 1 through 13
1 2 3 4 5 6 7 8 9 10 11 12 13

Columns 14 through 26

14 15 16 17 18 19 20 21 22 23 24 25 26

Columns 27 through 30

27 28 29 30

G =

25 22 19 16 13 10 7 4 1

H =

0 0.2000 0.4000 0.6000 1.6000

2. 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]

V1 =

1 2 3 4 5 6 7 8 9 0

V2 =

Columns 1 through 7

0.3000 1.2000 0.5000 2.1000 0.1000 0.4000 3.6000

Columns 8 through 10

4.2000 1.7000 0.9000

V3 =

4 4 4 4 3 3 2 2 2 1

(a)

V1+V2+V3

ans =
Columns 1 through 7

5.3000 7.2000 7.5000 10.1000 8.1000 9.4000 12.6000

Columns 8 through 10

14.2000 12.7000 1.9000

(b)

V1(5)
V2(5)
V3(5)

ans =

ans =

0.1000

ans =

Command V1(0) Error! Array indices must be positive integers or logical values.
Command V1(11) Error! Index exceeds array bounds.

(c)

V4=V2(1:5)
V5=V2(6:end)

V4 =

0.3000 1.2000 0.5000 2.1000 0.1000

V5 =

0.4000 3.6000 4.2000 1.7000 0.9000

(d)

V5=V2(6:end)
V6=[0.3 1.2 0.5 2.1 0.1 3.6 4.2 1.7 0.9]
V7=[0.3 1.2 0.5 2.1 0.1 0.4 1.4 4.2 1.7 0.9]
V8=V2(1:+2:9)

V6 =
Columns 1 through 7

0.3000 1.2000 0.5000 2.1000 0.1000 3.6000 4.2000


Columns 8 through 9

1.7000 0.9000

V7 =
Columns 1 through 7

0.3000 1.2000 0.5000 2.1000 0.1000 0.4000 1.4000

Columns 8 through 10

4.2000 1.7000 0.9000

V8 =
0.3000 0.5000 0.1000 3.6000 1.7000

(e) What are the results of

1. 9-V1
2. V1*5
3.V1+V2
4. V1-V3
5. V1.*V2
6. V1.^2
7. V1.^V3
8. V1==V3
9. V1>6
10. V1>V3
11. V3-(V1>2)
12. (V1>2)&(V1<6)
13. (V1>2)|(V1<6)
14. any(V1)
15. all(V1)

1. ans =

8 7 6 5 4 3 2 1 0 9

2. ans =

5 10 15 20 25 30 35 40 45 0

3. ans =

Columns 1 through 7

1.3000 3.2000 3.5000 6.1000 5.1000 6.4000 10.6000

Columns 8 through 10

12.2000 10.7000 0.9000

4. ans =
-3 -2 -1 0 2 3 5 6 7 -1

5. ans =

Columns 1 through 7

0.3000 2.4000 1.5000 8.4000 0.5000 2.4000 25.2000

Columns 8 through 10

33.6000 15.3000 0

6. ans =

1 4 9 16 25 36 49 64 81 0

7. ans =

1 16 81 256 125 216 49 64 81 0

8. ans =

1×10 logical array

0 0 0 1 0 0 0 0 0 0

9. ans =

1×10 logical array

0 0 0 0 0 0 1 1 1 0

10. ans =

1×10 logical array

0 0 0 0 1 1 1 1 1 0

11. ans =

4 4 3 3 2 2 1 1 1 1

12. ans =

1×10 logical array

0 0 1 1 1 0 0 0 0 0

13. ans =

1×10 logical array


1 1 1 1 1 1 1 1 1 1

14. ans =
logical
1

15. ans =
logical
0

4. Flow control

(a)

A=zeros(1,5);
for n=1:4
for m=1:3
A= A+n*m;
end
end
A

A =
60 60 60 60 60

(b)

B=[1 0]
if (all(B))
B=B+1;
elseif (any(B))
B=B+2;
else
B=B+3;
end
B

B =
1 0

B =
3 2

(c)

C=7:2:22
num=0;
while(all(C>0))
C=C-3;
num=num+1;
end
C
num

C =
7 9 11 13 15 17 19 21

C =
-2 0 2 4 6 8 10 12

num =
3

(d)

for i=1:20
H(i)=i*5;
end
H

H =
Columns 1 through 13

5 10 15 20 25 30 35 40 45 50 55 60 65

Columns 14 through 20

70 75 80 85 90 95 100

Yes the set of commands above have the same result as:

H = 1:20;
H = H*5;

for n=1:100000
x(n)=sin(n*pi/10);
end
n
x(n)

ans=
-4.1237e-12

5. Compare a script and a function

(a)

x=1:5;
y=6:10;
g=x+y;

(b)
function g = myfunction(x,y)

g=x+y;

(c)

myscript.m myfunction.m
C:\Users\xespaktech\Desktop\myscript.m C:\Users\xespaktech\Desktop\myfunction.m
Wed Jan 30 13:13:17 CST 2019 Wed Jan 30 13:13:17 CST 2019

1 x = 1:5; 1 x Function g = myfunction (x,y)


2 y = 6:10; 2 y <
3 g = x+y; 3 g x g = x + y;

(c)-1

myscript
x
y
g
z=myscript (error)

x =
1 2 3 4 5
y =
6 7 8 9 10
g =
7 9 11 13 15

Attempt to execute SCRIPT myscript as a function:


C:\Users\myscript.m
Error in filename (line 5)
z=myscript

(c)-3

z=myfunction(x,y)
g

z =
7 9 11 13 15
g =
7 9 11 13 15

a=1:10;
b=2:11;
myfunction(a,b)

ans =

3 5 7 9 11 13 15 17 19 21

You might also like