r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
ARREGLOS EN MATLAB
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
Arreglos
0 2 4 6 8
www.upibi.net
VS egu
e crida
t od en
r eI nstern et M a tL a b
Oriol Roca
Vectores
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
Arreglos
0 2 4 6 8
1 2 3 4 5
v(1)=0 v(2)=2
www.upibi.net
VS egu
e crida
t od en
r eI nstern et M a tL a b
Oriol Roca
-Para introducir un vector fila en MATLAB basta con escribir las componentes del
vector dentro de corchetes y separadas por espacios en blanco:
v=[0 2 4 6 8]
-Para introducir un vector columna se escriben las componentes dentro de
corchetes y separadas por el carcter punto y coma:
v=[0, 2, 4, 6, 8]
- En MATLAB es posible transformar un vector fila en un vector columna y
viceversa (hallar su traspuesta) empleando una comilla simple:
vt=v
www.upibi.net
VS egu
e crida
t od en
r eI nstern et M a tL a b
Oriol Roca
linspace(0,8,5)
Valor inicial Valor final Nmero de elementos
www.upibi.net
VS egu
e crida
t od en
r eI nstern et M a tL a b
Oriol Roca
Vectores
www.upibi.net
VS egu
e crida
t od en
r eI nstern et M a tL a b
Oriol Roca
Vectores
>> v(3)
= 4
Obtener porciones del vector es tan fcil como obtener elementos.
Basta con separar el primer nidice del ltimo con dos puntos
>> v(2:4)
=246
www.upibi.net
VS egu
e crida
t od en
r eI nstern et M a tL a b
Oriol Roca
Vectores
>> a = [1,2,3];
>> b = [4,5,6];
>> c=[a,b]
=123456
www.upibi.net
VS egu
e crida
t od en
r eI nstern et M a tL a b
Oriol Roca
Vectores
Algunas veces es necesario introducir un vector con componentes a intervalos
regulares. Esto se realiza fcilmente con MATLAB con la estructura
inicio:incremento:fin. Si no se proporciona un incremento, MATLAB asume que es
1.
>> x1=0:10
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
Leer Vector
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
Eliminar Vector
>>kt=[2 8 40 63 3 55 23 15 75 80]
kt =
2 8 40 63 3 55 23 15 75 80
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
Eliminar Vector
kt =
%el vector ahora tiene 5 elementos
2 8 15 75 80
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
A=[6 9 7 3 12 6 8]
A=
6 9 7 3 12 6 8
1 2 3 4 5 6 7
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
A=[6 9 7 3 12 6 8]
A=
6 9 7 3 12 6 8
1 2 3 4 5 6 7
acu=0;
vf=input('Cuantos elementos deseas? ');
for i=1:vf
fprintf('introduce el %g elemento del vector ',i);
vec(i)=input(' ');
acu= acu + vec(i);
end
disp(acu)
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
ans =
51
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
Vector fila
A=[1 2 3 4 5 6]
A=
1 2 3 4 5 6
Vector columna B=
B=[1; 2; 3; 4; 5; 6] 1
2
3
4
5
6
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
Tamao de un Vector
ans =
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
Ejercicio
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
Ejercicio
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
6 1 4 0 12
5 19 6 8 2
>>size(A)
ans= 2 5
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
B=reshape(A,3,2)
B=
5 0
8 6
1 2
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
A=
7 0 0
0 4 0
0 0 2
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
vec =
1
5
9
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
Ejercicios
Dado un vector, elemento a elemento, determina cul elemento es el mayor.
Ejemplo:
V= 2 9 -3 8
Em= 9
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
Ejercicios
Dado un vector, elemento a elemento, determina cul elemento es el mayor.
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
Ejercicios
n=5
vector= 1 0 1 0 1
--------
n=5
vector= 2 0 4 0 6
--------
n=4
vector= 1 4 9 16
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
Ejercicios
Ordenamiento de un vector por mtodo de la BURBUJA
El mtodo de la burbuja es uno de los mas simples, es tan fcil como
comparar todos los elementos de una lista contra todos, si se cumple que uno
es mayor o menor a otro, entonces los intercambia de posicin.
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
Ejercicios
Ordenamiento de un vector por mtodo de la BURBUJA
Por ejemplo, imaginemos que tenemos los siguientes valores:
5 6 1 0 3
Lo que hara una burbuja simple, seria comenzar recorriendo los valores de
izquierda a derecha, comenzando por el 5. Lo compara con el 6, con el 1, con
el 0 y con el 3, si es mayor o menor (dependiendo si el orden es ascendiente
o descendiente) se intercambian de posicin. Luego continua con el siguiente,
con el 6, y lo compara con todos los elementos de la lista, esperando ver si se
cumple o no la misma condicin que con el primer elemento. As,
sucesivamente, hasta el ultimo elemento de la lista.
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
MATRICES
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
MATRICES
Una matriz es, como el vector, una coleccin de elementos del mismo tipo.
Sin embargo, mientras el vector es unidimensional una sola fila o columna
en la matriz los datos se organizan en dos o ms dimensiones.
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
MATRICES
Las matrices se escriben como los vectores, pero separando las filas mediante
un punto y coma; as una matriz 3x3:
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
MATRICES
Son bidimensionales
1 1,1
2 1,2
3 1,3
4 2,1
5 2,2
6 2,3
7 3,1
8 3,2
9 3,3
X(1,1)= 1
X(1,2)=2
X(2,1)=4
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
MATRICES
Existen comandos que permiten crear de forma sencilla matrices.
Por ejemplo:
La matriz identidad
>>eye(5)
ans =
10000
01000
00100
00010
00001
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
MATRICES
Existen comandos que permiten crear de forma sencilla matrices.
Por ejemplo:
la matriz nula
>>zeros(3)
ans =
000
000
000
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
MATRICES
Existen comandos que permiten crear de forma sencilla matrices.
Por ejemplo:
o la matriz cuyos elementos valen todos 1:
>>ones(4)
ans =
1111
1111
1111
1111
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
MATRICES
Recorrido de un matriz
Para recorrer una matriz necesitamos dos bucles anidados. Por ejemplo, si
se desea recorrer todos los elementos de la matriz A por filas, se escribirn
dos sentencias for anidadas:
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
MATRICES
Ejemplo
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
MATRICES
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
MATRICES
El comando
>> P=[v1,v2,v3]
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
Multiplicacin de arreglos
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
Multiplicacin de arreglos
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
Multiplicacin de arreglos
www.upibi.net
AS egu
r rrida
e gd en
l oI nstern et M a tL a b
Oriol Roca
Ejercicios
Y valide que estas sean iguales para que la matriz sea cuadrada.
www.upibi.net