Professional Documents
Culture Documents
% abril 2015
filename=volumen.res";
% abre archivo para escritura
fid = fopen(filename, "w");
r=0;
while r < 100
r=input(' introduce el radio = ');
disp(r)
vol = pi *r^3;
if(r < 0) break;end;
fprintf(fid, 'vol= %12.3f\n',vol);
fprintf('vol = %12.3f \n',vol);
end
$ cierra archivo
fclose(fid);
Row 1
Row 2
Row 3
arr(3,2)
Row 4
Col 1 Col 2 Col 3 Col 4 Col 5
2
Obtain information
size(A): return [m n]
length(A): length of a vector
length(A) = max(size(A))
B = A(2:4,3:5)
B is the subset of A from row 2 to row 4, column 3
to column 5
A(:, 2)=[]
Delete second column
Row vectors
a = [1 2 3] or a = [1, 2, 3]
e.g. V = [1 3 sqrt(5)],
what is length(V)?
- build a row vector from existing ones: e.g. w = [1 2 3],
z = [8, 9],
cd = [2*z -w],
sort(cd) (ascending order)
- look at value of particular entries:
e.g. w(2) = ?
set w(3) = 100, then w = ??
Column vectors
e.g. c = [1; 3; sqrt(5)]
Multidimensional Arrays
Multidimensional Arrays
A two dimensional array with m rows and n columns
will occupy mxn successive locations in the computers
memory.
a= [1 2 3; 4 5 6; 7 8 9; 10 11 12];
1
a(5) = a(1,2) = 2
A 2x3x2 array of three dimensions
c(:, :, 1) = [1 2 3; 4 5 6 ];
c(:, :, 2) = [7 8 9; 10 11 12];
4
1
10
10 11 12
5
8
11
10
Examples
To enter a matrix
2 5
6 4
3
1
>> A = [2 5 3; 6 4 1]
>> B = [1:1.5:6; 2 3 4 5] ?
>> for i=1:4
for j=1:3
C(i,j)=i*j;
end
end
>> D =[]; D=[D;5]; D=[D;6;7]
>> E = zeros(4, 5)
11
Save/Load Data
save fname
Save all workspace data into fname.mat
Save fname x y z
Save(fname): when fname is a variable
Load fname
load fname x y
12
Ejercicio: p--.m
En un archivo p--.m, construya la siguiente matriz:
X=
1 2 3
4 5 6
7 8 9
1) Calcule el sin(x)
2) Evale de nuevo utilizando instruccin for
Ejercicio: p--.m
En un archivo p--.m, construya las siguientes matrices
A=
B=
0.1 0.2 0.3
0.3 0.1 0.2
0.4 0.5 0.6
0.9 0.7 0.8
0.7 0.8 0.9
0.4 0.6 0.5
Obtenga:
1) C= A+B ; C= A-B; C=A.*B; C=A./B; C=A.^3
2) Evale utilizando instruccin for
13