Operar con variables.
Definir una variable:
>> A = 7
A=
7
Definir una matriz:
>> B = [1 2 3; 4 5 6; 7 8 9]
B=
123
456
789
Comprobar el estado de una variable:
>> B
B=
123
456
789
Acceder a un elemento de una matriz: tomando la matriz B, queremos acceder al valor de
la posicin (1,2).
>> B(1,2)
ans =
2
>> B(:,1) % Para obtener todos los elementos de la columna 1.
ans =
1
4
7
>> B(1,:) % Para obtener todos los elementos de la fila 1.
ans =
123
Mostrar componentes consecutivos:
>> B(1:2,1)
ans =
1
4
Aadir elementos a una matriz:
>> B(4,1) = -1
B=
123
456
789
-1 0 0
Producto escalar:
>> A = [1 2;3 4]
A=
12
34
>> B = [4 3; 2 1]
B=
43
21
>> A(1,1)*B(1,1)
ans =
4
Producto matricial:
>> A*B
ans =
85
20 13
Sentencias de Control.
FOR
>> for i=1:3
display('hola mundo')
end
ans =
hola mundo
ans =
hola mundo
ans =
hola mundo
WHILE
>> i = 1;
while i < 3
display('hola mundo')
i = i+1;
end
ans =
hola mundo
ans =
hola mundo
IF
>> a
a=
1
>> b
b=
1
>> if (a==b)
display('hola mundo')
end
ans =
hola mundo
REPRESENTACIN GRAFICA
>> a = 0:0.1:4*pi
a=
Columns 1 through 9
0
0.1000
0.2000
0.3000
0.4000
0.5000
0.6000
0.7000
0.8000
Columns 10 through 18
0.9000
1.0000
1.1000
1.2000
1.3000
1.4000
1.5000
1.6000
1.7000
2.1000
2.2000
2.3000
2.4000
2.5000
2.6000
3.0000
3.1000
3.2000
3.3000
3.4000
3.5000
3.9000
4.0000
4.1000
4.2000
4.3000
4.4000
4.8000
4.9000
5.0000
5.1000
5.2000
5.3000
5.7000
5.8000
5.9000
6.0000
6.1000
6.2000
Columns 19 through 27
1.8000
1.9000
2.0000
Columns 28 through 36
2.7000
2.8000
2.9000
Columns 37 through 45
3.6000
3.7000
3.8000
Columns 46 through 54
4.5000
4.6000
4.7000
Columns 55 through 63
5.4000
5.5000
5.6000
Columns 64 through 72
6.3000
6.4000
6.5000
6.6000
6.7000
6.8000
6.9000
7.0000
7.1000
7.5000
7.6000
7.7000
7.8000
7.9000
8.0000
8.4000
8.5000
8.6000
8.7000
8.8000
8.9000
9.3000
9.4000
9.5000
9.6000
9.7000
9.8000
Columns 73 through 81
7.2000
7.3000
7.4000
Columns 82 through 90
8.1000
8.2000
8.3000
Columns 91 through 99
9.0000
9.1000
9.2000
Columns 100 through 108
9.9000 10.0000 10.1000 10.2000 10.3000 10.4000 10.5000 10.6000 10.7000
Columns 109 through 117
10.8000 10.9000 11.0000 11.1000 11.2000 11.3000 11.4000 11.5000 11.6000
Columns 118 through 126
11.7000 11.8000 11.9000 12.0000 12.1000 12.2000 12.3000 12.4000 12.5000
>> plot(a,sin(a))
Mostrar por pantalla
>> a = 1
a=
1
>> sprintf('Valor de a: %d',a)
ans =
Valor de a: 1
Inversa de una matriz
>> f=[1 2;3 4]
f=
1 2
3 4
>> a=inv(f)
a=
-2.0000 1.0000
1.5000 -0.5000