0% found this document useful (0 votes)
6 views9 pages

Vectores en Matlab: Comandos y Ejemplos

The document provides a comprehensive guide on using vectors in Matlab, detailing various operations such as indexing, mathematical functions, and logical operations. It includes examples of vector manipulation, including sorting, filtering, and performing calculations like sums and averages. Additionally, it covers advanced topics like plotting and weighted averages, showcasing the versatility of Matlab for vector analysis.

Uploaded by

pezolua
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Vectores en Matlab: Comandos y Ejemplos

The document provides a comprehensive guide on using vectors in Matlab, detailing various operations such as indexing, mathematical functions, and logical operations. It includes examples of vector manipulation, including sorting, filtering, and performing calculations like sums and averages. Additionally, it covers advanced topics like plotting and weighted averages, showcasing the versatility of Matlab for vector analysis.

Uploaded by

pezolua
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MA713 - FIQT - UNI - 2025 Vectores en Matlab Mg.

Miguel Sierra

%Se uso el comando diary nombraarchivo, para registrar


%todos los comandos usados

%vectores
x=[3 1,7 6 1+1 5]
x =
3 1 7 6 2 5
x(2)+3
ans =
4
x
x =
3 1 7 6 2 5
x(2)=5
x =
3 5 7 6 2 5
x(2)=1
x =
3 1 7 6 2 5
3:6
ans =
3 4 5 6
3:6.5
ans =
3 4 5 6
3.3:6
ans =
3.3000 4.3000 5.3000
3:2:6
ans =
3 5
5:-1:2
ans =
5 4 3 2
x(2:5)
ans =
1 7 6 2
%longitud del vector: length
length(x)
ans =
6
x(2:6)
ans =
1 7 6 2 5
x(2:length(x))
ans =
1 7 6 2 5
x(2:end)
ans =
1 7 6 2 5
x(end:-1:1)
ans =

1/9
MA713 - FIQT - UNI - 2025 Vectores en Matlab Mg. Miguel Sierra

5 2 6 7 1 3
x
x =
3 1 7 6 2 5
x=x(end:-1:1)
x =
5 2 6 7 1 3
x=x(end:-1:1) %regresamos
x =
3 1 7 6 2 5
%Invertir el vector: fliplr, flipud,
fliplr(x)
ans =
5 2 6 7 1 3
x
x =
3 1 7 6 2 5
%Operaciones globales sobre el vector
%sum, prod, max, min, mean
sum(x)
ans =
24
mean(x)
ans =
4
%Otras operaciones vectoriales
%cumsum, cumprod, sort
x
x =
3 1 7 6 2 5
cumsum(x)
ans =
3 4 11 17 19 24
sort(x)
ans =
1 2 3 5 6 7
x
x =
3 1 7 6 2 5
help sort

x
x =
3 1 7 6 2 5
sort(x,"descend")
ans =
7 6 5 3 2 1
sort(x)
ans =
1 2 3 5 6 7
sort(-x)
ans =

2/9
MA713 - FIQT - UNI - 2025 Vectores en Matlab Mg. Miguel Sierra

-7 -6 -5 -3 -2 -1
-sort(-x)
ans =
7 6 5 3 2 1
x
x =
3 1 7 6 2 5
factorial(3)
ans =
6
factorial(x)
ans =
6 1 5040 720 2 120
y=2:7
y =
2 3 4 5 6 7
x
x =
3 1 7 6 2 5
x*y
{Error }
y'
ans =
2
3
4
5
6
7
x*y'
ans =
114
x
x =
3 1 7 6 2 5
y
y =
2 3 4 5 6 7
%Operaciones por elementos
% .* ./ .\ .^
x.*y
ans =
6 3 28 30 12 35
x.^y
ans =
9 1 2401 7776 64 78125
x^2
{Error}
x.^2
ans =
9 1 49 36 4 25
x

3/9
MA713 - FIQT - UNI - 2025 Vectores en Matlab Mg. Miguel Sierra

x+1
ans =
4 2 8 7 3 6
x
x =
3 1 7 6 2 5
y
y =
2 3 4 5 6 7
%Operadores relacionales
% > < >= <= == ~=
% alt 38: &, alt 124: |, alt 126: ~
4>2
ans =
1
y>4
ans =
0 0 0 1 1 1
% operadores lógicos:
% y, and, & o, or, | no, not, ~
x>4
ans =
0 0 1 1 0 1
(x>4) & (x<7)
ans =
0 0 0 1 0 1
clc
x
x =
3 1 7 6 2 5
z=[3+2 x(3) x(3:5) x(end)]
z =
5 7 7 6 2 5
%eliminar elementos
[]
ans =
[]
x(4)=[]
x =
3 1 7 2 5
x(1:2:end)=[]
x =
1 2
x=[3 1,7 6 1+1 5]
x =
3 1 7 6 2 5
x=[x 3]
x =
3 1 7 6 2 5 3
length(x)
ans =
7

4/9
MA713 - FIQT - UNI - 2025 Vectores en Matlab Mg. Miguel Sierra

x(9)
{Index exceeds the number of array elements (7)}
x(9)=4
x =
3 1 7 6 2 5 3 0 4
x(7:9)=[]
x =
3 1 7 6 2 5
%Cuantos elementos de x son >3
x>3
ans =
0 0 1 1 0 1
sum(x>3) %cuantos son
ans =
3
%Sumar los elementos >3
x.*(x>3)
ans =
0 0 7 6 0 5
sum(x.*(x>3))
ans =
18
%residuo de la division entera, funcion mod
mod(11,4)
ans =
3
mod(12,4)
ans =
0
mod(11.5,4.1)
ans =
3.3000
x
x =
3 1 7 6 2 5
%cuantos elementos de x son multiplos de 3
mod(x,3)
ans =
0 1 1 0 2 2
mod(x,3)==0
ans =
1 0 0 1 0 0
sum(mod(x,3)==0)
ans =
2
clc
%Cuales son los >3
x
x =
3 1 7 6 2 5
x.*(x>3)
ans =

5/9
MA713 - FIQT - UNI - 2025 Vectores en Matlab Mg. Miguel Sierra

0 0 7 6 0 5
%filtros
x(x>3)
ans =
7 6 5
x.*(x>3)
ans =
0 0 7 6 0 5
x(x==0)=[]
x =
3 1 7 6 2 5
x(x.*(x>3)==0)=[]
x =
7 6 5
x
x =
7 6 5
x=[3 1,7 6 1+1 5]
x =
3 1 7 6 2 5
y
y =
2 3 4 5 6 7
x(y>5)
ans =
2 5
x
x =
3 1 7 6 2 5
x([0 0 0 1 0 1]) %no funciona
x([2 5 1])
ans =
1 2 3
clc
%n es el vector de notas de cursos
%c son los creditos de los cursos
n=[15 8 16 9];
c=[4 2 5 3];
%Cuantos cursos aprobó
sum(n>=10)
ans =
2
%cuantos creditos aprobó
n>=10
ans =
1 0 1 0
c.*(n>=10)
ans =
4 0 5 0
sum(c.*(n>=10))
ans =
9

6/9
MA713 - FIQT - UNI - 2025 Vectores en Matlab Mg. Miguel Sierra

c(n>=10)
ans =
4 5
sum(c(n>=10))
ans =
9
%promedio ponderado
sum(n.*c)/sum(c)
ans =
13.0714
pp=sum(n.*c)/sum(c)
pp =
13.0714
%redondeos round, ceil, floor, fix
floor(pp)
ans =
13
floor(pp,2)
{Error Too many input arguments.}
round(pp,2)
ans =
13.0700
floor(pp*100)
ans =
1307
floor(pp*100)/100
ans =
13.0700
clc
pp=sum(n.*c)/sum(c)
pp =
13.0714
%promedio ponderado solo de aprobados
n
n =
15 8 16 9
c
c =
4 2 5 3
a=n(n>=10)
a =
15 16
c=c(n>=10)
c =
4 5
n
n =
15 8 16 9
c=[4 2 5 3];
a=n(n>=10)
a =
15 16

7/9
MA713 - FIQT - UNI - 2025 Vectores en Matlab Mg. Miguel Sierra

b=c(n>=10)
b =
4 5
ppa=sum(a.*b)/sum(b)
ppa =
15.5556
ppa= sum(n(n>=10).*c(n>=10))/sum(c(n>=10))
ppa =
15.5556
clc

%prob 4
x
x =
3 1 7 6 2 5
(sum(x)-min(x))/(length(x)-1)
ans =
4.6000
y=sort(x)
y =
1 2 3 5 6 7
mean(y(2:end))
ans =
4.6000
clc
%prob 5
y=sort(x)
y =
1 2 3 5 6 7
mean(y(3:end))
ans =
5.2500
clc
%graficar con plot(x,y)
x=0:0.05:5;
x=linspace(0,5,100);
longitud=(5-0)/99
longitud =
0.0505
x=linspace(0,5);
exp(1)
ans =
2.7183
y=exp(3*x)-sin(x)+x.^0.3;
plot(x,y)
clc
%prob 14
N=6;
a=1:N;
num=2.^a;
den=a.^a;
t=num./den;

8/9
MA713 - FIQT - UNI - 2025 Vectores en Matlab Mg. Miguel Sierra

t
t =
Columns 1 through 4
2.0000 1.0000 0.2963 0.0625
Columns 5 through 6
0.0102 0.0014
t(2:2:end)=-t(2:2:end)
t =
Columns 1 through 4
2.0000 -1.0000 0.2963 -0.0625
Columns 5 through 6
0.0102 -0.0014
S=sum(t)
S =
1.2427
clc

%prob 14
N=6;
a=1:N;
num=2.^a;
den=a.^a;
t=num./den;
t(2:2:end)=-t(2:2:end);
S=sum(t)
S =
1.2427
clc
%prob 12
x=1:6
x =
1 2 3 4 5 6
k=3
k =
3
x=[x(k+1:end) x(1:k)]
x =
4 5 6 1 2 3
clc
%prob 17
n=4;
F=[2 0 5 1];
C=[1 3 2 1];
V=[1 2 1 2];
P=[3 0 3 2]; %Hallarlo con operaciones
(F>C)*3
ans =
3 0 3 0
(F>C)*3+(F==C).*V
ans =
3 0 3 2
P=(F>C)*3+(F==C).*V

9/9

You might also like