Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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 =
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
Columns 8 through 10
V3 =
4 4 4 4 3 3 2 2 2 1
(a)
V1+V2+V3
ans =
Columns 1 through 7
Columns 8 through 10
(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 =
V5 =
(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
1.7000 0.9000
V7 =
Columns 1 through 7
Columns 8 through 10
V8 =
0.3000 0.5000 0.1000 3.6000 1.7000
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
Columns 8 through 10
4. ans =
-3 -2 -1 0 2 3 5 6 7 -1
5. ans =
Columns 1 through 7
Columns 8 through 10
33.6000 15.3000 0
6. ans =
1 4 9 16 25 36 49 64 81 0
7. ans =
8. ans =
0 0 0 1 0 0 0 0 0 0
9. ans =
0 0 0 0 0 0 1 1 1 0
10. ans =
0 0 0 0 1 1 1 1 1 0
11. ans =
4 4 3 3 2 2 1 1 1 1
12. ans =
0 0 1 1 1 0 0 0 0 0
13. ans =
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
(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
(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
(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