573 lines
13 KiB
Plaintext
573 lines
13 KiB
Plaintext
octave:2> z = zeros(3)
|
|
z =
|
|
|
|
0 0 0
|
|
0 0 0
|
|
0 0 0
|
|
|
|
octave:3> mean(1:5)
|
|
ans = 3
|
|
octave:4> zeros(3)
|
|
ans =
|
|
|
|
0 0 0
|
|
0 0 0
|
|
0 0 0
|
|
|
|
octave:5> zeros(3, 1)
|
|
ans =
|
|
|
|
0
|
|
0
|
|
0
|
|
|
|
octave:6> help sum
|
|
'sum' is a built-in function from the file libinterp/corefcn/data.cc
|
|
|
|
-- sum (X)
|
|
-- sum (X, DIM)
|
|
-- sum (..., "native")
|
|
-- sum (..., "double")
|
|
-- sum (..., "extra")
|
|
Sum of elements along dimension DIM.
|
|
|
|
If DIM is omitted, it defaults to the first non-singleton
|
|
dimension.
|
|
|
|
The optional "type" input determines the class of the variable used
|
|
for calculations. By default, operations on floating point inputs
|
|
(double or single) are performed in their native data type, while
|
|
operations on integer, logical, and character data types are
|
|
performed using doubles. If the argument "native" is given, then
|
|
the operation is performed in the same type as the original
|
|
argument.
|
|
|
|
For example:
|
|
|
|
sum ([true, true])
|
|
=> 2
|
|
sum ([true, true], "native")
|
|
=> true
|
|
|
|
If "double" is given the sum is performed in double precision even
|
|
for single precision inputs.
|
|
|
|
For double precision inputs, the "extra" option will use a more
|
|
accurate algorithm than straightforward summation. For single
|
|
precision inputs, "extra" is the same as "double". For all other
|
|
data type "extra" has no effect.
|
|
|
|
See also: cumsum, sumsq, prod.
|
|
|
|
Additional help for built-in functions and operators is
|
|
available in the online version of the manual. Use the command
|
|
'doc <topic>' to search the manual index.
|
|
|
|
Help and information about Octave is also available on the WWW
|
|
at https://www.octave.org and via the help@octave.org
|
|
mailing list.
|
|
octave:7> sum(eye(3))
|
|
ans =
|
|
|
|
1 1 1
|
|
|
|
octave:8> eye(3)
|
|
ans =
|
|
|
|
Diagonal Matrix
|
|
|
|
1 0 0
|
|
0 1 0
|
|
0 0 1
|
|
|
|
octave:9> sum(eye(3))
|
|
ans =
|
|
|
|
1 1 1
|
|
|
|
octave:10> sum(eye(3), 2)
|
|
ans =
|
|
|
|
1
|
|
1
|
|
1
|
|
|
|
octave:11> sum(eye(3), 3)
|
|
ans =
|
|
|
|
1 0 0
|
|
0 1 0
|
|
0 0 1
|
|
|
|
octave:12> hold on
|
|
octave:13> close all
|
|
octave:14> close("all")
|
|
octave:15> hold("on")
|
|
octave:16> close("all")
|
|
octave:17> figure
|
|
octave:18> figure()
|
|
octave:19> close("all")
|
|
octave:20> hist(1:10)
|
|
octave:21> close all
|
|
octave:22> a = hist(1:10)
|
|
a =
|
|
|
|
1 1 1 1 1 1 1 1 1 1
|
|
|
|
octave:23> help find
|
|
'find' is a built-in function from the file libinterp/corefcn/find.cc
|
|
|
|
-- IDX = find (X)
|
|
-- IDX = find (X, N)
|
|
-- IDX = find (X, N, DIRECTION)
|
|
-- [i, j] = find (...)
|
|
-- [i, j, v] = find (...)
|
|
Return a vector of indices of nonzero elements of a matrix, as a
|
|
row if X is a row vector or as a column otherwise.
|
|
|
|
To obtain a single index for each matrix element, Octave pretends
|
|
that the columns of a matrix form one long vector (like Fortran
|
|
arrays are stored). For example:
|
|
|
|
find (eye (2))
|
|
=> [ 1; 4 ]
|
|
|
|
If two inputs are given, N indicates the maximum number of elements
|
|
to find from the beginning of the matrix or vector.
|
|
|
|
If three inputs are given, DIRECTION should be one of "first" or
|
|
"last", requesting only the first or last N indices, respectively.
|
|
However, the indices are always returned in ascending order.
|
|
|
|
If two outputs are requested, 'find' returns the row and column
|
|
indices of nonzero elements of a matrix. For example:
|
|
|
|
[i, j] = find (2 * eye (2))
|
|
=> i = [ 1; 2 ]
|
|
=> j = [ 1; 2 ]
|
|
|
|
If three outputs are requested, 'find' also returns a vector
|
|
containing the nonzero values. For example:
|
|
|
|
[i, j, v] = find (3 * eye (2))
|
|
=> i = [ 1; 2 ]
|
|
=> j = [ 1; 2 ]
|
|
=> v = [ 3; 3 ]
|
|
|
|
If X is a multi-dimensional array of size m x n x p x ..., J
|
|
contains the column locations as if X was flattened into a
|
|
two-dimensional matrix of size m x (n + p + ...).
|
|
|
|
Note that this function is particularly useful for sparse matrices,
|
|
as it extracts the nonzero elements as vectors, which can then be
|
|
used to create the original matrix. For example:
|
|
|
|
sz = size (a);
|
|
[i, j, v] = find (a);
|
|
b = sparse (i, j, v, sz(1), sz(2));
|
|
|
|
See also: nonzeros.
|
|
|
|
Additional help for built-in functions and operators is
|
|
available in the online version of the manual. Use the command
|
|
'doc <topic>' to search the manual index.
|
|
|
|
Help and information about Octave is also available on the WWW
|
|
at https://www.octave.org and via the help@octave.org
|
|
mailing list.
|
|
octave:24> cube = @(x) x.^3;
|
|
octave:25> cube
|
|
cube =
|
|
|
|
@(x) x .^ 3
|
|
|
|
octave:26> cube(3)
|
|
ans = 27
|
|
octave:27> paraboloid = @(x, y) x.^2 + y.^2;
|
|
octave:28> paraboloid(3, 4)
|
|
ans = 25
|
|
octave:29> @(x) x.^3
|
|
ans =
|
|
octave:35> a = 9
|
|
a = 9
|
|
octave:36> capture = @() a;
|
|
octave:37> capture
|
|
capture =
|
|
|
|
@() a
|
|
|
|
octave:38> capture()
|
|
ans = 9
|
|
octave:39> capture = @() 9
|
|
capture =
|
|
|
|
@() 9
|
|
|
|
octave:40> capture()
|
|
ans = 9
|
|
octave:41> capture = @() a;
|
|
octave:42> capture()
|
|
ans = 9
|
|
octave:43> a = 3
|
|
a = 3
|
|
octave:44> capture()
|
|
ans = 9
|
|
octave:45> n = 4
|
|
n = 4
|
|
octave:46> n = 2
|
|
n = 2
|
|
octave:47> pown = @(x) x.^n
|
|
pown =
|
|
|
|
@(x) x .^ n
|
|
|
|
octave:48> pown(3)
|
|
ans = 9
|
|
octave:49> n = 4
|
|
n = 4
|
|
octave:50> pown(3)
|
|
ans = 9
|
|
octave:51>
|
|
^[[200~compose = @(f, g) @(x) f(g(x));^[[201~octave:51>
|
|
octave:51>
|
|
octave:51>
|
|
octave:51>
|
|
octave:51> compose = @(f, g) @(x) f(g(x));
|
|
octave:52>
|
|
octave:52>
|
|
octave:52>
|
|
octave:52>
|
|
octave:52>
|
|
octave:52> add_three = @(x) x + 3;
|
|
times_four = @(x) x * 4;
|
|
x_times_four_plus_three = compose(add_three, times_four);
|
|
octave:55> x_times_four_plus_three
|
|
x_times_four_plus_three =
|
|
|
|
@(x) f (g (x))
|
|
|
|
octave:56> function y = times_four_plus_three(x)
|
|
> x2 = x.*4;
|
|
> y = x2 + 3;
|
|
> end
|
|
octave:57> times_four_plus_three(1)
|
|
ans = 7
|
|
octave:58> answer = times_four_plus_three(1)
|
|
answer = 7
|
|
octave:59> function [y, z] = mystery_math(p, q)
|
|
> y = p + q;
|
|
> z = p - q;
|
|
> disp('did the math!');
|
|
> end
|
|
octave:60> mystery_math(3, 4)
|
|
did the math!
|
|
ans = 7
|
|
octave:61> [val1, val2] = mystery_math(3, 4)
|
|
did the math!
|
|
val1 = 7
|
|
val2 = -1
|
|
octave:62>
|
|
|
|
[cat@lazarus:~/classes/ece210-materials/2024-van-west/lessons/lesson04]
|
|
$ ls
|
|
BasicClass.m datatypes.m for_loops.m lecture.txt
|
|
controlflow.m distance.m funnnnnn.m
|
|
[cat@lazarus:~/classes/ece210-materials/2024-van-west/lessons/lesson04]
|
|
$ cat distance.m
|
|
% In a file like this one (which only defines normal functions, and in which
|
|
% the first function is hononymous with the filename), the first function is a
|
|
% public function, and all other functions are local helper functions.
|
|
%
|
|
% Note that you DO NOT want to put clc; clear; close all; at the top of a file
|
|
% like this; it only makes sense to do this at the top of script files when you
|
|
% want to clean your workspace!
|
|
|
|
% Calculate Euclidean distance
|
|
% A public function, callable by other files and from the Command Window if
|
|
% this is in the MATLAB path.
|
|
function d = distance(x, y)
|
|
d = sqrt(sq(x) + sq(y));
|
|
end
|
|
|
|
% A local helper function. Only accessible within this file.
|
|
function x = sq(x)
|
|
x = x * x;
|
|
end
|
|
[cat@lazarus:~/classes/ece210-materials/2024-van-west/lessons/lesson04]
|
|
$ octave
|
|
GNU Octave, version 7.3.0
|
|
Copyright (C) 1993-2022 The Octave Project Developers.
|
|
This is free software; see the source code for copying conditions.
|
|
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or
|
|
FITNESS FOR A PARTICULAR PURPOSE. For details, type 'warranty'.
|
|
|
|
Octave was configured for "x86_64-pc-linux-gnu".
|
|
|
|
Additional information about Octave is available at https://www.octave.org.
|
|
|
|
Please contribute if you find this software useful.
|
|
For more information, visit https://www.octave.org/get-involved.html
|
|
|
|
Read https://www.octave.org/bugs.html to learn how to submit bug reports.
|
|
For information about changes from previous versions, type 'news'.
|
|
|
|
octave:1> who
|
|
octave:2> distance(3, 4)
|
|
ans = 5
|
|
octave:3> path
|
|
|
|
Octave's search path contains the following directories:
|
|
|
|
.
|
|
/usr/lib/x86_64-linux-gnu/octave/7.3.0/site/oct/x86_64-pc-linux-gnu
|
|
/usr/lib/x86_64-linux-gnu/octave/site/oct/api-v57/x86_64-pc-linux-gnu
|
|
/usr/lib/x86_64-linux-gnu/octave/site/oct/x86_64-pc-linux-gnu
|
|
/usr/share/octave/7.3.0/site/m
|
|
/usr/share/octave/site/api-v57/m
|
|
/usr/share/octave/site/m
|
|
/usr/share/octave/site/m/startup
|
|
/usr/lib/x86_64-linux-gnu/octave/7.3.0/oct/x86_64-pc-linux-gnu
|
|
/usr/share/octave/7.3.0/m
|
|
/usr/share/octave/7.3.0/m/audio
|
|
/usr/share/octave/7.3.0/m/deprecated
|
|
/usr/share/octave/7.3.0/m/elfun
|
|
/usr/share/octave/7.3.0/m/general
|
|
/usr/share/octave/7.3.0/m/geometry
|
|
/usr/share/octave/7.3.0/m/gui
|
|
/usr/share/octave/7.3.0/m/help
|
|
/usr/share/octave/7.3.0/m/image
|
|
/usr/share/octave/7.3.0/m/io
|
|
/usr/share/octave/7.3.0/m/java
|
|
/usr/share/octave/7.3.0/m/legacy
|
|
/usr/share/octave/7.3.0/m/linear-algebra
|
|
/usr/share/octave/7.3.0/m/miscellaneous
|
|
/usr/share/octave/7.3.0/m/ode
|
|
/usr/share/octave/7.3.0/m/optimization
|
|
/usr/share/octave/7.3.0/m/path
|
|
/usr/share/octave/7.3.0/m/pkg
|
|
/usr/share/octave/7.3.0/m/plot
|
|
/usr/share/octave/7.3.0/m/plot/appearance
|
|
/usr/share/octave/7.3.0/m/plot/draw
|
|
/usr/share/octave/7.3.0/m/plot/util
|
|
/usr/share/octave/7.3.0/m/polynomial
|
|
/usr/share/octave/7.3.0/m/prefs
|
|
/usr/share/octave/7.3.0/m/profiler
|
|
/usr/share/octave/7.3.0/m/set
|
|
/usr/share/octave/7.3.0/m/signal
|
|
/usr/share/octave/7.3.0/m/sparse
|
|
/usr/share/octave/7.3.0/m/specfun
|
|
/usr/share/octave/7.3.0/m/special-matrix
|
|
/usr/share/octave/7.3.0/m/startup
|
|
/usr/share/octave/7.3.0/m/statistics
|
|
/usr/share/octave/7.3.0/m/strings
|
|
/usr/share/octave/7.3.0/m/testfun
|
|
/usr/share/octave/7.3.0/m/time
|
|
/usr/share/octave/7.3.0/m/web
|
|
/usr/share/octave/7.3.0/data
|
|
|
|
octave:4> power_to_dB = @(p) 10*log10(p);
|
|
octave:5> dB_to_power = @(db) 10.^(db./10);
|
|
octave:6> power_to_dB(10)
|
|
ans = 10
|
|
octave:7> power_to_dB(1000)
|
|
ans = 30
|
|
octave:8> power_to_dB(100)
|
|
ans = 20
|
|
octave:9> dB_to_power(10)
|
|
ans = 10
|
|
octave:10> dB_to_power(-10)
|
|
ans = 0.1000
|
|
octave:11> for v = [1 2 5 7]
|
|
> v
|
|
> end
|
|
v = 1
|
|
v = 2
|
|
v = 5
|
|
v = 7
|
|
octave:12> for i = 1:10
|
|
> i + 3
|
|
> end
|
|
ans = 4
|
|
ans = 5
|
|
ans = 6
|
|
ans = 7
|
|
ans = 8
|
|
ans = 9
|
|
ans = 10
|
|
ans = 11
|
|
ans = 12
|
|
ans = 13
|
|
octave:13> total = 0
|
|
total = 0
|
|
octave:14> for i = 1:10
|
|
> total = total + i
|
|
> end
|
|
total = 1
|
|
total = 3
|
|
total = 6
|
|
total = 10
|
|
total = 15
|
|
total = 21
|
|
total = 28
|
|
total = 36
|
|
total = 45
|
|
total = 55
|
|
octave:15> sum(1:10)
|
|
ans = 55
|
|
octave:16> 1 < 2
|
|
ans = 1
|
|
octave:17> who
|
|
Variables visible from the current scope:
|
|
|
|
ans dB_to_power i power_to_dB total v
|
|
|
|
octave:18> whos
|
|
Variables visible from the current scope:
|
|
|
|
variables in scope: top scope
|
|
|
|
Attr Name Size Bytes Class
|
|
==== ==== ==== ===== =====
|
|
ans 1x1 1 logical
|
|
dB_to_power 1x1 0 function_handle
|
|
i 1x1 8 double
|
|
power_to_dB 1x1 0 function_handle
|
|
total 1x1 8 double
|
|
v 1x1 8 double
|
|
|
|
Total is 6 elements using 25 bytes
|
|
|
|
octave:19> if 1 < 2
|
|
> disp('math still works today');
|
|
> else
|
|
> disp('oh no');
|
|
> end
|
|
math still works today
|
|
octave:20> i = 0
|
|
i = 0
|
|
octave:21> while i < 10
|
|
> i = i + 1
|
|
> end
|
|
i = 1
|
|
i = 2
|
|
i = 3
|
|
i = 4
|
|
i = 5
|
|
i = 6
|
|
i = 7
|
|
i = 8
|
|
i = 9
|
|
i = 10
|
|
octave:22> try
|
|
> a = [1 2; 3 4];
|
|
> b = [1 2 3 4];
|
|
> c = a*b;
|
|
> catch err
|
|
> disp('yo! don't do that!')
|
|
error: parse error:
|
|
|
|
syntax error
|
|
|
|
>>> disp('yo! don't do that!')
|
|
^
|
|
octave:22> try
|
|
> a = [1 2; 3 4];
|
|
> b = [1 2 3 4];
|
|
> c = a*b;
|
|
> catch err
|
|
> disp('yo! do not do that!')
|
|
> end
|
|
yo! do not do that!
|
|
octave:23> a
|
|
a =
|
|
|
|
1 2
|
|
3 4
|
|
|
|
octave:24> b
|
|
b =
|
|
|
|
1 2 3 4
|
|
|
|
octave:25> a*b
|
|
error: operator *: nonconformant arguments (op1 is 2x2, op2 is 1x4)
|
|
octave:26> clear
|
|
octave:27> whos
|
|
octave:28> a = 10
|
|
a = 10
|
|
octave:29> class(a)
|
|
ans = double
|
|
octave:30> b = int8(a)
|
|
b = 10
|
|
octave:31> class(b)
|
|
ans = int8
|
|
octave:32> b + .5
|
|
ans = 11
|
|
octave:33> whos
|
|
Variables visible from the current scope:
|
|
|
|
variables in scope: top scope
|
|
|
|
Attr Name Size Bytes Class
|
|
==== ==== ==== ===== =====
|
|
a 1x1 8 double
|
|
ans 1x1 1 int8
|
|
b 1x1 1 int8
|
|
|
|
Total is 3 elements using 10 bytes
|
|
|
|
octave:34>
|
|
octave:34>
|
|
octave:34>
|
|
^[[200~imshow(uint8(rand(128, 128, 3)));^[[201~octave:34>
|
|
octave:34>
|
|
octave:34> imshow(uint8(rand(128, 128, 3)));
|
|
octave:35> close all
|
|
octave:36>
|
|
^[[200~imshow(double(rand(128, 128, 3)));^[[201~octave:36>
|
|
octave:36>
|
|
octave:36>
|
|
octave:36>
|
|
octave:36> imshow(double(rand(128, 128, 3)));
|
|
octave:37> close all
|
|
octave:38> imshow(uint8(rand(128, 128, 3)));
|
|
octave:39> close all
|
|
octave:40> imshow(uint8(rand(128, 128, 3)));
|
|
octave:40> eps
|
|
ans = 2.2204e-16
|
|
octave:41> eps(1)
|
|
ans = 2.2204e-16
|
|
octave:42> eps(.001)
|
|
ans = 2.1684e-19
|
|
octave:43> eps(10000)
|
|
ans = 1.8190e-12
|
|
octave:44> eps(1e100)
|
|
ans = 1.9427e+84
|
|
octave:45> 10^(10^10)
|
|
ans = Inf
|
|
octave:46> a = 5
|
|
a = 5
|
|
octave:47> b = "hello!"
|
|
b = hello!
|
|
octave:48> s = struct('a', 5, 'b', "hello!")
|
|
s =
|
|
|
|
scalar structure containing the fields:
|
|
|
|
a = 5
|
|
b = hello!
|
|
|
|
octave:49> s
|
|
s =
|
|
|
|
scalar structure containing the fields:
|
|
|
|
a = 5
|
|
b = hello!
|
|
|
|
octave:50> s.a
|
|
ans = 5
|
|
octave:51> s.b
|
|
ans = hello!
|
|
octave:52>
|