You are on page 1of 13

% Escribe a archivo

% 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);

Variables and Arrays


Array: A collection of data values organized into rows
and columns, and known by a single name.

Row 1
Row 2
Row 3
arr(3,2)

Row 4
Col 1 Col 2 Col 3 Col 4 Col 5
2

Variables and Arrays


Arrays
The fundamental unit of data in octave
Scalars are also treated as arrays by octave (1
row and 1 column).
Row and column indices of an array start from 1.
Arrays can be classified as vectors and
matrices.

Variables and Arrays


Vector: Array with one dimension
Matrix: Array with more than one dimension
Size of an array is specified by the number of rows
and the number of columns, with the number of
rows mentioned first (For example: n x m array).
Total number of elements in an array is the
product of the number of rows and the number of
columns.

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)]

column notation : a shortcut for producing row


vectors
e.g.
1:100
3:7
5:0.1:6
1:-1 --> []
0.32:0.1:0.6
-0.4:-0.3:-2
7

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

Basic Mathematical Operations


Remember that every variable can be a matrix!
Addition:
>> C = A + B
Subtraction:
>> D = A B
Multiplication:
>> E = A * B (Matrix multiplication)
>> E = A .* B (Element wise multiplication, A and B same size)
Division:
Left Division and Right Division
>> F = A . / B (Element wise division)
>> F = A / B = A*inv(B) (A * inverse of B)
>> F = A . \ B (Element wise division)
>> F = A \ B=inv(A)*B
(inverse of A * B)
9

Generating basic matrices


Matrix with ZEROS:
>> A = zeros(m, n)
Matrix with ONES:
>> B = ones(m, n)
IDENTITY Matrix:
>> I = eye(m, n)
m Rows
n Columns
zeros, ones, eye Octave functions

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

You might also like