0% found this document useful (0 votes)
16 views

Clase04 Matrices Matlab

This document discusses matrices in MATLAB. It shows how to create, manipulate, and perform operations on matrices. Some key points covered include: - Creating matrices with values and assigning their size - Performing element-wise operations like addition and exponentiation on matrices - Finding the maximum, minimum, sum, mean and other values of matrices - Sorting and flipping matrices - Finding indices of maximum values - Creating identity, ones and magic matrices

Uploaded by

CarlosCristobal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Clase04 Matrices Matlab

This document discusses matrices in MATLAB. It shows how to create, manipulate, and perform operations on matrices. Some key points covered include: - Creating matrices with values and assigning their size - Performing element-wise operations like addition and exponentiation on matrices - Finding the maximum, minimum, sum, mean and other values of matrices - Sorting and flipping matrices - Finding indices of maximum values - Creating identity, ones and magic matrices

Uploaded by

CarlosCristobal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

>> %Clase de matrices en Matlab

>> a=[3 4
7 1]
a =
3
4
7
1
>> x=[1:3;4:6]
x =
1
2
3
4
5
6
>> %length(vector)
tamano
>> length(x)
ans =
3
>> [f c]=size(x)
f =
2
c =
3
>> a
a =
3
4
7
1
>> b=3*a
b =
9
12
21
3
>> a./b
ans =
0.3333
0.3333
0.3333
0.3333
>> a.\b
ans =
3
3
3
3
>> b.^a
ans =
1.0e+009 *
0.0000
0.0000
1.8011
0.0000
>> format long
>> b.^a
ans =
1.0e+009 *
0.000000729000000
0.000020736000000
1.801088541000000
0.000000003000000
%Resolver:
3x+2y=7
X+4y=13
>> A=[3 2;1 4]
A =
3
2
1
4
>> b=[7 13]'
b =

7
13
>> sol=A\b
sol =
0.200000000000000
3.200000000000000
>> A^-1*b
ans =
0.200000000000000
3.200000000000000
>> format
>> %manipular
>> a
a =
3
4
7
1
>> a(2,1)=8
a =
3
4
8
1
>> a(2,1)=7
a =
3
4
7
1
>> a(3,4)
??? Index exceeds matrix dimensions.
>> a(3,4)=5
a =
3
4
0
0
7
1
0
0
0
0
0
5
>> a=[a;1:4]
a =
3
4
0
0
7
1
0
0
0
0
0
5
1
2
3
4
>> a=[a [2:5]']
a =
3
4
0
0
2
7
1
0
0
3
0
0
0
5
4
1
2
3
4
5
>> a(2,5)
ans =
3
>> a(2:4,5)
ans =
3
4
5
>> a
a =
3
4
0
0
2
7
1
0
0
3
0
0
0
5
4
1
2
3
4
5
>> a(2:4,1:5)

ans =

7
1
0
0
1
2
>> a(2:4,1:end)
ans =
7
1
0
0
1
2
>> a(2:4,:)
ans =
7
1
0
0
1
2
>> a([3 1 4],:)
ans =
0
0
3
4
1
2
>> a
a =
3
4
7
1
0
0
1
2
>> a(4:-1:1,:)
ans =
1
2
0
0
7
1
3
4
>> a
a =
3
4
7
1
0
0
1
2
>> flipud(a)
ans =
1
2
0
0
7
1
3
4
>> a'
ans =
3
7
4
1
0
0
0
0
2
3
>> fliplr(a)
ans =
2
0
3
0
4
5
5
4
>> a
a =

0
0
3

0
5
4

3
4
5

0
0
3

0
5
4

3
4
5

0
0
3

0
5
4

3
4
5

0
0
3

5
0
4

4
2
5

0
0
0
3

0
0
5
4

2
3
4
5

3
0
0
0

4
5
0
0

5
4
3
2

0
0
0
3

0
0
5
4

2
3
4
5

3
0
0
0

4
5
0
0

5
4
3
2

0
0
0
5
4

1
2
3
4
5

0
0
0
3

4
1
0
2

3
7
0
1

3
4
0
7
1
0
0
0
0
1
2
3
>> a(:,3)=[]
a =
3
4
0
7
1
0
0
0
5
1
2
4
>> b=a
b =
3
4
0
7
1
0
0
0
5
1
2
4
>> a([1 3],:)=[]
a =
7
1
0
1
2
4
>> a=b
a =
3
4
0
7
1
0
0
0
5
1
2
4
>> eye(3)
ans =
1
0
0
0
1
0
0
0
1
>> ones(2,3)
ans =
1
1
1
1
1
1
>> magic(3)
ans =
8
1
6
3
5
7
4
9
2
>> a
a =
3
4
0
7
1
0
0
0
5
1
2
4
>> sum(a)
ans =
11
7
9
>> max(a)
ans =
7
4
5
>> mean(a)
ans =
2.7500
1.7500
>> sort(a)
ans =
0
0
0

0
0
5
4

2
3
4
5

2
3
4
5
2
3
4
5
3
5
2
3
4
5

2
3
4
5
14
5
2.2500
2

3.5000

1
1
0
3
2
4
7
4
5
>> -sort(-a)
ans =
7
4
5
3
2
4
1
1
0
0
0
0
>> cumsum(a)
ans =
3
4
0
10
5
0
10
5
5
11
7
9
>> a
a =
3
4
0
7
1
0
0
0
5
1
2
4
>> factorial(a)
ans =
6
5040
1
1
>> sum(sum(a))
ans =
41
>> max(max(a))
ans =
7
>> x=[ 2 4 6 1]
x =
2
4
6
>> [m i]=max(x)
m =
6
i =
3
>> x(1)=6
x =
6
4
6
>> [m i]=max(x)
m =
6
i =
1
>> p=find(x==max(x))
p =
1
3
>> x(p)
ans =
6
6
>> x
x =
6
4
6

3
4
5
5
4
3
2
2
5
9
14
2
3
4
5
24
1
1
2

1
1
120
24

2
6
24
120

>> x(x==max(x))
ans =
6
6
>> x(x==max(x))=x(x==max(x))+1
x =
7
4
7
1
>> a
a =
3
4
0
2
7
1
0
3
0
0
5
4
1
2
4
5
>> a(a>5)
ans =
7
>> a(a>=5)
ans =
7
5
5
>> p=find(a>=5)
p =
2
11
16
>> a
a =
3
4
0
2
7
1
0
3
0
0
5
4
1
2
4
5
>> a(4)
ans =
1
>> a(6)
ans =
1
>> a
a =
3
4
0
2
7
1
0
3
0
0
5
4
1
2
4
5
>> [f c]=find(a>=5)
f =
2
3
4
c =
1
3
4
>> a(a>=5)=a(a>=5)+2
a =
3
4
0
2
9
1
0
3
0
0
7
4
1
2
4
7

>> z=ones(8);
>> z(1:2:end,2:2:end)=0;
>> z(2:2:end,1:2:end)=0
z =
1
0
1
0
0
1
0
1
1
0
1
0
0
1
0
1
1
0
1
0
0
1
0
1
1
0
1
0
0
1
0
1
>> z=ones(5);
>> z(1:2:end,2:2:end)=0;
>> z(2:2:end,1:2:end)=0
z =
1
0
1
0
0
1
0
1
1
0
1
0
0
1
0
1
1
0
1
0
>> z=ones(5);
>> z(2:2:25)=0
z =
1
0
1
0
0
1
0
1
1
0
1
0
0
1
0
1
1
0
1
0

1
0
1
0
1
0
1
0

0
1
0
1
0
1
0
1

1
0
1
0
1
0
1
0

0
1
0
1
0
1
0
1

1
0
1
0
1

1
0
1
0
1

%Matriz de notas. Las filas son alumnos y las columnas prcticas.


>> p=[3 4 2 5 3;2 1 0 7 8; 6 2 6 5 3;7 1 2 4 2]
p =
3
4
2
5
3
2
1
0
7
8
6
2
6
5
3
7
1
2
4
2
>> %no del alumno con la mas alta nota en alguna pc
>> [f c]=find(p==max(max(p)));
>> f
f =
2
>> %anadir una columna con el prom. de practicas eliminando la menor
>> p
p =
3
4
2
5
3
2
1
0
7
8
6
2
6
5
3
7
1
2
4
2
>> sum(p)
ans =
18
8
10
21
16
>> sum(p')
ans =
17
18
22
16
>> sum(p')'
ans =
17

18
22
16

>> p
p =

3
4
2
5
3
2
1
0
7
8
6
2
6
5
3
7
1
2
4
2
>> pp=(sum(p')-min(p'))/4
pp =
3.7500
4.5000
5.0000
3.7500
>> p=[p pp']
p =
3.0000
4.0000
2.0000
5.0000
2.0000
1.0000
0
7.0000
6.0000
2.0000
6.0000
5.0000
7.0000
1.0000
2.0000
4.0000
>> % No del alumno con mas alto pp
>> [m i]=max(p(:,6))
m =
5
i =
3
>> i;
>> [fi ci]=find(p(:,6)==max(p(:,6)))
fi =
3
ci =
1
>> fi;
>> a
a =
3
4
0
2
9
1
0
3
0
0
7
4
1
2
4
7
>> [m i]=max(a)
m =
9
4
7
7
i =
2
1
3
>> [m i]=max(a')
m =
4
9
7

4
7

i =
2

3.0000
8.0000
3.0000
2.0000

3.7500
4.5000
5.0000
3.7500

You might also like