0% found this document useful (0 votes)
56 views20 pages

Operaciones Con Matrices Y Vectores

This document discusses operations with matrices and vectors in MATLAB. It shows examples of creating and manipulating matrices and vectors using basic arithmetic operations, transposes, inverses, and various MATLAB functions. It also covers defining matrices using ranges and other methods, as well as empty matrices and deleting rows or columns. Finally, it lists some common elementary mathematical functions in MATLAB that operate elementwise on arrays, such as trigonometric, inverse trigonometric, logarithmic and exponential functions.

Uploaded by

Jhoel Asto
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)
56 views20 pages

Operaciones Con Matrices Y Vectores

This document discusses operations with matrices and vectors in MATLAB. It shows examples of creating and manipulating matrices and vectors using basic arithmetic operations, transposes, inverses, and various MATLAB functions. It also covers defining matrices using ranges and other methods, as well as empty matrices and deleting rows or columns. Finally, it lists some common elementary mathematical functions in MATLAB that operate elementwise on arrays, such as trigonometric, inverse trigonometric, logarithmic and exponential functions.

Uploaded by

Jhoel Asto
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/ 20

OPERACIONES CON MATRICES Y VECTORES

>> x=-4:.01:4; y=sin(x); plot(x,y), grid, title('Funcin seno(x)')

>> A=[1 2 3; 4
A=
1
4
7

2
5
8

3
6
9

4
5
6

7
8
9

>> A'
ans =
1
2
3

>> B=A'
B=
1
2
3

4
5
6

>> B*A

7
8
9

5 6; 7 8 9]

ans =
66 78 90
78 93 108
90 108 126
>> A=[1 4 -3; 2 1 5; -2 5 3]
A=
1
2
-2

4
1
5

-3
5
3

>> B=inv(A)
B=
0.1803
0.1311
-0.0984

0.2213 -0.1885
0.0246 0.0902
0.1066 0.0574

>> B*A
ans =
1.0000 0.0000 -0.0000
0 1.0000 0.0000
0 -0.0000 1.0000
>> x=[10 20 30] % vector fila
x=
10

20

30

>> y=[11; 12; 13] % vector columna


y=
11
12
13
>> x+y'
ans =
21 32 43
>> x(1)=1, x(2)=2
x=
1

x=
1 2

OPERADORES ARITMTICOS
>> A=[1 2; 3 4]
A=
1
2
3
4
>> A*2
ans =
2
6

4
8

>> A-4
ans =
-3
-1

-2
0

TIPOS DE DATOS
>> 1.0/0.0
ans =
Inf
>> 0/0
ans =
NaN
>> inf/inf
ans =
NaN

>> i=int32(100); % se crea un entero de 4 bytes con valor 100


>> j=zeros(100); i=int32(j); % se crea un entero i a partir de j
>> i=zeros(1000,1000,'int32'); % se crea una mariz 1000x1000 de enteros
>> disp([intmin('int64'), intmax('int64')])
-9223372036854775808 9223372036854775807

>> n=1000; AA=rand(n); A=single(AA);


>> tic, Bs=inv(A); toc
Elapsed time is 0.365748 seconds.
>> tic, Bd=inv(AA); toc
Elapsed time is 0.408784 seconds.
>> A=magic(4)
A=
16
2
5 11
9
7
4 14

3 13
10
8
6 12
15
1

>> j=A>10
j=
1
0
0
0

0
1
0
1

0
0
0
1

1
0
1
0

>> isa(j,'logical')
ans =
1
>> A(j)=-10
A=
-10
2
3 -10
5 -10 10
8
9
7
6 -10
4 -10 -10
1

NMEROS COMPLEJOS: FUNCIN COMPLEX


>> a=sqrt(-4)
a=
0 + 2.0000i
>> 3 + 4j
ans =
3.0000 + 4.0000i
>> i=2

i=
2
>> 2+3i
ans =
2.0000 + 3.0000i
>> 2+3*i
ans =
8
>> 2+3*j
ans =
2.0000 + 3.0000i
>> A = [1+2i 2+3i; -1+i 2-3i]
A=
1.0000 + 2.0000i 2.0000 + 3.0000i
1.0000
2.0000 - 3.0000i
>> A = [1 2; -1 2] + [2 3; 1 -3]*i % En este caso el * es necesario
A=
5
1

8
-4

>> complex(1,2)
ans =
1.1

+ 2.0000i

OTRAS FORMAS DE DEFINIR MATRICES


>> A=rand(3)
A=
0.9040
0.9409
0.8025

0.2420
0.9757
0.3172

>> B=diag(diag(A))

0.8128
0.6974
0.2695

B=
0.9040
0
0
0 0.9757
0
0
0 0.2695
>> C=[A, eye(3); zeros(3), B]
C=
0.9040
0.9409
0.8025
0
0
0

0.2420 0.8128 1.0000


0
0
0.9757 0.6974
0 1.0000
0
0.3172 0.2695
0
0 1.0000
0
0 0.9040
0
0
0
0
0 0.9757
0
0
0
0
0 0.2695

>> x=1:10
x=
1

10

>> x=1:2:10
x=
1

>> x=1:1.5:10
x=
1.0000

2.5000

4.0000

5.5000

7.0000

>> x=10:-1:1
x=
10

>> x=[0.0:pi/50:2*pi]';
>> y=sin(x); z=cos(x);
>> [x y z]
ans =
0
0.0628
0.1257
0.1885
0.2513
0.3142

0 1.0000
0.0628 0.9980
0.1253 0.9921
0.1874 0.9823
0.2487 0.9686
0.3090 0.9511

8.5000 10.0000

0.3770
0.4398
0.5027
0.5655
0.6283
0.6912
0.7540
0.8168
0.8796
0.9425
1.0053
1.0681
1.1310
1.1938
1.2566
1.3195
1.3823
1.4451
1.5080
1.5708
1.6336
1.6965
1.7593
1.8221
1.8850
1.9478
2.0106
2.0735
2.1363
2.1991
2.2619
2.3248
2.3876
2.4504
2.5133
2.5761
2.6389
2.7018
2.7646
2.8274
2.8903
2.9531
3.0159
3.0788
3.1416
3.2044
3.2673
3.3301
3.3929
3.4558
3.5186
3.5814
3.6442
3.7071

0.3681
0.4258
0.4818
0.5358
0.5878
0.6374
0.6845
0.7290
0.7705
0.8090
0.8443
0.8763
0.9048
0.9298
0.9511
0.9686
0.9823
0.9921
0.9980
1.0000
0.9980
0.9921
0.9823
0.9686
0.9511
0.9298
0.9048
0.8763
0.8443
0.8090
0.7705
0.7290
0.6845
0.6374
0.5878
0.5358
0.4818
0.4258
0.3681
0.3090
0.2487
0.1874
0.1253
0.0628
0.0000
-0.0628
-0.1253
-0.1874
-0.2487
-0.3090
-0.3681
-0.4258
-0.4818
-0.5358

0.9298
0.9048
0.8763
0.8443
0.8090
0.7705
0.7290
0.6845
0.6374
0.5878
0.5358
0.4818
0.4258
0.3681
0.3090
0.2487
0.1874
0.1253
0.0628
-0.0000
-0.0628
-0.1253
-0.1874
-0.2487
-0.3090
-0.3681
-0.4258
-0.4818
-0.5358
-0.5878
-0.6374
-0.6845
-0.7290
-0.7705
-0.8090
-0.8443
-0.8763
-0.9048
-0.9298
-0.9511
-0.9686
-0.9823
-0.9921
-0.9980
-1.0000
-0.9980
-0.9921
-0.9823
-0.9686
-0.9511
-0.9298
-0.9048
-0.8763
-0.8443

3.7699
3.8327
3.8956
3.9584
4.0212
4.0841
4.1469
4.2097
4.2726
4.3354
4.3982
4.4611
4.5239
4.5867
4.6496
4.7124
4.7752
4.8381
4.9009
4.9637
5.0265
5.0894
5.1522
5.2150
5.2779
5.3407
5.4035
5.4664
5.5292
5.5920
5.6549
5.7177
5.7805
5.8434
5.9062
5.9690
6.0319
6.0947
6.1575
6.2204
6.2832

-0.5878
-0.6374
-0.6845
-0.7290
-0.7705
-0.8090
-0.8443
-0.8763
-0.9048
-0.9298
-0.9511
-0.9686
-0.9823
-0.9921
-0.9980
-1.0000
-0.9980
-0.9921
-0.9823
-0.9686
-0.9511
-0.9298
-0.9048
-0.8763
-0.8443
-0.8090
-0.7705
-0.7290
-0.6845
-0.6374
-0.5878
-0.5358
-0.4818
-0.4258
-0.3681
-0.3090
-0.2487
-0.1874
-0.1253
-0.0628
-0.0000

-0.8090
-0.7705
-0.7290
-0.6845
-0.6374
-0.5878
-0.5358
-0.4818
-0.4258
-0.3681
-0.3090
-0.2487
-0.1874
-0.1253
-0.0628
-0.0000
0.0628
0.1253
0.1874
0.2487
0.3090
0.3681
0.4258
0.4818
0.5358
0.5878
0.6374
0.6845
0.7290
0.7705
0.8090
0.8443
0.8763
0.9048
0.9298
0.9511
0.9686
0.9823
0.9921
0.9980
1.0000

>> A=magic(6)
A=
35
1
3 32
31
9
8 28
30
5
4 36
>> A(2,3)

6 26 19 24
7 21 23 25
2 22 27 20
33 17 10 15
34 12 14 16
29 13 18 11

ans =
7
>> A(6, 1:4)
ans =
4

36

29

13

>> A(3, :)
ans =
31

22

27

20

>> A(end, :)
ans =
4

36

29

13

18

11

>> A(3:5,:)
ans =
31
9
8 28
30
5

2 22 27 20
33 17 10 15
34 12 14 16

>> A([1 2 5],:)


ans =
35
1
3 32
30
5

6 26 19 24
7 21 23 25
34 12 14 16

>> B=eye(size(A));
>> B([2 4 5],:)=A(1:3,:)
B=
1
0
35
1
0
0
3 32
31
9
0
0

0
6
1
7
2
0

0
0
0
26 19 24
0
0
0
21 23 25
22 27 20
0
0
1

>> B=eye(size(A));
>> B(1:2,:)=[0 1; 1 0]*B(1:2,:)

B=
0
1
0
0
0
0

1
0
0
0
0
0

0
0
1
0
0
0

0
0
0
1
0
0

0
0
0
0
1
0

0
0
0
0
0
1

>> x=rand(1,5)
x=
0.5896

0.8330

0.3638

0.7098

0.3383

0.3638

0.8330

0.5896

>> x=x(5:-1:1)
x=
0.3383

0.7098

>> A=magic(3)
A=
8
3
4

1
5
9

6
7
2

>> A(:,3:-1:1)
ans =
6
7
2

1
5
9

8
3
4

MATRIZ VACA A[ ]. BORRADO DE FILAS O COLUMNAS


>> A=magic(3)
A=
8
3
4

1
5
9

>> B=[]
B=
[]
>> exist(B)

6
7
2

ans =
0
>> isempty(B)
ans =
1
>> A(:,3)=[]
A=
8
3
4

1
5
9

FUNCIONES MATEMTICAS ELEMENTALES QUE OPERAN DE MODO


ESCALAR
sin(x)
seno
cos(x)
coseno
tan(x)
tangente
asin(x)
arco seno
acos(x)
arco coseno
atan(x)
arco tangente (devuelve un ngulo entre -p/2 y
+p/2)
atan2(x)
arco tangente (devuelve un ngulo entre -p y +p);
se le pasan 2 argumentos,proporcionales al seno y
al coseno
sinh(x)
seno hiperblico
cosh(x)
coseno hiperblico
tanh(x)
tangente hiperblica
asinh(x)
arco seno hiperblico
acosh(x)
arco coseno hiperblico
atanh(x)
arco tangente hiperblica
log(x)
logaritmo natural
log10(x)
logaritmo decimal
exp(x)
funcin exponencial
sqrt(x)
raz cuadrada
sign(x)
devuelve -1 si <0, 0 si =0 y 1 si >0. Aplicada a un
nmero complejo,devuelve un vector unitario en la
misma direccin
rem(x,y)
resto de la divisin (2 argumentos que no tienen
que ser enteros)
mod(x,y)
similar a rem (Ver diferencias con el Help)
round(x)
redondeo hacia el entero ms prximo
fix(x)
redondea hacia el entero ms prximo a 0
floor(x)
valor entero ms prximo hacia -8
ceil(x)
valor entero ms prximo hacia +8
gcd(x)
mximo comn divisor
lcm(x)
mnimo comn mltiplo

real(x)
imag(x)
abs(x)
angle(x)

partes reales
partes imaginarias
valores absolutos
ngulos de fase

>> A=magic(3)
A=
8
3
4

1
5
9

6
7
2

>> M=A>4
M=
1
0
0

0
1
1

1
1
0

>> A=magic(3)
A=
8
3
4

1
5
9

6
7
2

>> m=find(A>4)
m=
1
5
6
7
8
>> A(m)=10*ones(size(m))
A=
10
1
3 10
4 10

10
10
2

>> any(A==3)
ans =
1

>> any(ans)
ans =
1
>> all(all(A))
ans =
1
>> x=[1 2 3 4 0/0 6]
x=
1

4 NaN

>> i=find(isnan(x))
i=
5
>> x=x(find(~isnan(x)))
x=
1

>> x=x(~isnan(x))
x=
1

>> x(isnan(x))=[]
x=
1

>> A(any(isnan(A)'), :)=[]


A=
10
1
3 10
4 10

10
10
2

FUNCIONES PARA CLCULOS CON POLINOMIOS


>> pol=[1 0 -8 6 -10]

pol =
1

-8

6 -10

>> roots(pol)
ans =
-3.2800
2.6748
0.3026 + 1.0238i
0.3026 - 1.0238i
>> polyval(pol,1)
ans =
-11
>> pol1=[1 -2 4]
pol1 =
1

-2

>> pol2=[1 0 3 -4]


pol2 =
1

-4

>> pol3=conv(pol1,pol2)
pol3 =
1 -2
7 -10 20 -16
>> syms x
>> y=sin(x); z=cos(x);
>> simplify(y^2+z^2)
ans =
1
>> limit(sin(x)/x,0)
ans =
1
>> syms n
>> limit((n^3+3*n^2-2*n)/(3*n^3-1),inf)

ans =
1/3
>> f='exp(z^3)+sin(z)^2'
f=
exp(z^3)+sin(z)^2
>> diff(f)
ans =
2*cos(z)*sin(z) + 3*z^2*exp(z^3)
>> f='a*exp(a+x)/sin(a*x)'
f=
a*exp(a+x)/sin(a*x)

>> diff(f,'a')
ans =
exp(a + x)/sin(a*x) + (a*exp(a + x))/sin(a*x) - (a*x*exp(a +
x)*cos(a*x))/sin(a*x)^2
>> pretty(ans)
exp(a + x) a exp(a + x) a x exp(a + x) cos(a x)
---------- + ------------ - ----------------------sin(a x)
sin(a x)
2
sin(a x)
>> f='a*exp(a*x)';
>> int(f,'x')
ans =
exp(a*x)
>> int(f,'x',0,1)
ans =
exp(a) - 1
>> int('exp(x^2)','x',0,1)
ans =

-(pi^(1/2)*erf(i)*i)/2
>> f=inline('exp(x.^2)');
>> quad(f,0,1)
ans =
1.4627
>>

quad('x.*exp(x)',0,1)

ans =
1.0000

GRFICOS

BIDIMENSIONALES
>> x=[1 3 2 4 5 3]
x=
1

>> plot(x)

>> x=[1 6 5 2 1]; y=[1 0 4 3 1];


>> plot(x,y)

>> x=0:pi/25:6*pi;
>> y=sin(x); z=cos(x);
>> plot(x,y,x,z)

>>

z=eig(rand(20,20));
>> plot(real(z),imag(z),'+')

>> x=[-

4*pi:pi/20:4*pi];
>> plot(x,sin(x),'r',x,cos(x),'g')
>> title('Funcin seno(x) -en rojo- y funcin coseno(x) -en verde-')
>> xlabel('ngulo en radianes'), figure(gcf)
>> ylabel('valor de la funcin trigonomtrica'), figure(gcf)
>> axis([-12,12,-1.5,1.5]), figure(gcf)
>> axis('equal'), figure(gcf)
>> axis('normal'), figure(gcf)
>> axis('square'), figure(gcf)

>> axis('off'), figure(gcf)


>> axis('on'), figure(gcf)
>> axis('on'), grid, figure(gcf)

>>
>>
>>
>>
>>
>>
>>
>>

x=[rand(1,100)*10];
plot(x)
bar(x)
stairs(x)
hist(x)
hist(x,20)
alfa=(rand(1,20)-0.5)*2*pi;
rose(alfa)

You might also like