Sie sind auf Seite 1von 11

PRÁCTICA N°03

INGRESO DE DATOS A UNA MATRIZ Y GENERACIÓN DE VECTORES


3.1 INGRESO DE DATOS A UNA MATRIZ
Ejemplo.
𝑎11 𝑎12 𝑏11 𝑏12
Hacer un programa para calcular A+B donde 𝐴 = [ ], 𝐵 = [ ]
𝑎21 𝑎22 𝑏21 𝑏22
Solución
>> a11=input ('ingrese el valor de a11:'); a12=input('ingrese el valor de
a12:');a21=input('ingrese el valor de a21:');a22=input('ingrese el valor de a22:');A=[a11
a12;a21 a22]
ingrese el valor de a11:2
ingrese el valor de a12:3
ingrese el valor de a21:4
ingrese el valor de a22:5

A=

2 3
4 5
>> b11=input ('ingrese el valor de b11:');b12=input('ingrese el valor
deb12:');b21=input('ingrese el valor de b21:');b22=input('ingrese el valor de
b22:');B=[b11 b12;b21 b22]
ingrese el valor de b11:6
ingrese el valor de b12:7
ingrese el valor de b21:8
ingrese el valor de b22:9

B=

6 7
8 9
>> C=A+B
C=
8 10
12 14
Ejercicio. Hacer un programa en Matlab para ingresar datos a una matriz y realizar la
𝐴+𝐵−𝐶
siguiente operación |2𝐴+𝐵−2𝐶|

1 −2 3 4 3 2 4 8 9
Donde 𝐴 = [−1 2 3] ; 𝐵 = [ 6 5 2] ; 𝐶 = [−2 4 6]
6 5 4 −1 2 7 −1 7 8
Solución
>> a1=input('ingrese el valor de a1:');a2=input('ingrese el valor de
a2:');a3=input('ingrese el valor de a3:');a4=input('ingrese el valor de
a4:');a5=input('ingrese el valor de a5:');a6=input('ingrese el valor de
a6:');a7=input('ingrese el valor de a7:');a8=input('ingrese el valor de
a8:');a9=input('ingrese el valor de a9:');A=[a1 a2 a3;a4 a5 a6;a7 a8 a9]
ingrese el valor de a1:1
ingrese el valor de a2:-2
ingrese el valor de a3:3
ingrese el valor de a4:-1
ingrese el valor de a5:2
ingrese el valor de a6:3
ingrese el valor de a7:6
ingrese el valor de a8:5
ingrese el valor de a9:4

A=

1 -2 3
-1 2 3
6 5 4
>> b1=input('ingrese el valor de b1:');b2=input('ingrese el valor de
b2:');b3=input('ingrese el valor de b3:');b4=input('ingrese el valor de
b4:');b5=input('ingrese el valor de b5:');b6=input('ingrese el valor de
b6:');b7=input('ingrese el valor de b7:');b8=input('ingrese el valor de
b8:');b9=input('ingrese el valor de b9:');B=[b1 b2 b3;b4 b5 b6;b7 b8 b9]
ingrese el valor de b1:4
ingrese el valor de b2:3
ingrese el valor de b3:2
ingrese el valor de b4:6
ingrese el valor de b5:5
ingrese el valor de b6:2
ingrese el valor de b7:-1
ingrese el valor de b8:2
ingrese el valor de b9:7
B=

4 3 2
6 5 2
-1 2 7
>> c1=input('ingrese el valor de c1:');c2=input('ingrese el valor de
c2:');c3=input('ingrese el valor de c3:');c4=input('ingrese el valor de
c4:');c5=input('ingrese el valor de c5:');c6=input('ingrese el valor de
c6:');c7=input('ingrese el valor de c7:');c8=input('ingrese el valor de
c8:');c9=input('ingrese el valor de c9:');C=[c1 c2 c3;c4 c5 c6;c7 c8 c9]
ingrese el valor de c1:4
ingrese el valor de c2:8
ingrese el valor de c3:9
ingrese el valor de c4:-2
ingrese el valor de c5:4
ingrese el valor de c6:6
ingrese el valor de c7:-1
ingrese el valor de c8:7
ingrese el valor de c9:8
C=

4 8 9
-2 4 6
-1 7 8
>> D=A+B-C
D=

1 -7 -4
7 3 -1
6 0 3

>> E=2*A+B-2*C
E=

-2 -17 -10
8 1 -4
13 -2 -1

>> F=D./E
F=

-0.5000 0.4118 0.4000


0.8750 3.0000 0.2500
0.4615 0 -3.0000

>> det(F)
ans =
5.0745
3.2 GENERACIÓN DE VECTORES
Formatos útiles para definir vectores.
Un vector de n elementos se puede representar de la siguiente manera.
𝑉 = [𝑣1 ; 𝑣2 ; … ; 𝑣𝑛 ]
Forma general
Variable = vector arbitrario

3.2.1. Variable = [a: b]


Define un vector cuyos elementos están entre a y b, y sus elementos intermedios se
diferencian en una unidad.
Ejemplo.
>> v1= [2:4]
v1 =
2 3 4
>> v1(2)
ans =
3
>> V1(3)
ans =
4
Ejercicio. Calcular en Matlab las siguientes operaciones:
a) 𝑉1 ∗ 𝑉2 − 𝑉3
Solución
>> V1=[4:10]
V1 =
4 5 6 7 8 9 10

>> V2=[24:30]
V2 =
24 25 26 27 28 29 30

>> V3=[8:14]
V3 =
8 9 10 11 12 13 14
>> V=V1.*V2-V3
V=
88 116 146 178 212 248 286
b) 2𝑉1∗𝑉2∗𝑉3
>> 2.^(V1.*V2.*V3)
ans =
1.0e+231 *
1.5525 Inf Inf Inf Inf Inf Inf

3.2.2. Variable = [a:d:b]


Define un vector cuyos elementos están entre a y no necesaria el b, y sus elementos
intermedios se diferencian en d unidades.
Ejemplo.
>> V4=[2:4:14]
V4 =
2 6 10 14

>> V5=[4:3:17]
V5 =
4 7 10 13 16

>> V6=[10:-2:5]
V6 =
10 8 6
Ejercicio. Calcular en Matlab las siguientes operaciones.
a) 𝑉1 + 𝑉2 + 𝑉3
>> V1=[4:2:12]
V1 =
4 6 8 10 12

>> V2=[10:3:23]
V2 =
10 13 16 19 22

>> V3=[22:-4:5]
V3 =
22 18 14 10 6

>> V=V1+V2-V3
V=
-8 1 10 19 28

b) 𝑉1 ∗ 𝑉2 + 𝑉3𝑉1+𝑉2∗𝑉3
>> U=(V1.*V2+V3).^(V1+V2.*V3)
U=
Inf Inf Inf Inf Inf

3.2.3. Variable = linspace (a, b, n)


Define un vector cuyos elementos están de a hasta b y tienen elementos igualmente
espaciados.
Ejemplo.
>> V5=linspace(10,30,6)
V5 =
10 14 18 22 26 30

>> V6=linspace(0,10,5)
V6 =
0 2.5000 5.0000 7.5000 10.0000

Ejercicio. Calcular en Matlab las siguientes operaciones.


(𝑉1+𝑉2−𝑉3)
a) 𝑉1∗𝑉2+𝑉3
>> V1=linspace(12,20,5)

V1 =

12 14 16 18 20

>> V2=linspace(6,12,5)

V2 =

6.0000 7.5000 9.0000 10.5000 12.0000

>> V3=linspace(22,30,5)

V3 =

22 24 26 28 30

>> U=V1+V2-V3

U=

-4.0000 -2.5000 -1.0000 0.5000 2.0000

>> W=V1.*V2+V3

W=

94 129 170 217 270

>> H=U/W

H=

-0.0013

>> H=U./W

H=

-0.0426 -0.0194 -0.0059 0.0023 0.0074


b) 3𝑉1∗𝑉2−𝑉3
>> 3.^(V1.*V2-V3)
ans =
1.0e+100 *

0.0000 0.0000 0.0000 0.0000 1.5684

3.2.4 Transpuesta de un vector fila o columna y matrices

Cambio de filas por columnas de viceversa.

Ejemplo: Ejercicio:
3.2.5 Concatenación de vectores y matrices

Ejemplo
Ejercicio Concatenas las siguientes matrices

Das könnte Ihnen auch gefallen