Skip to content

Instantly share code, notes, and snippets.

@npodonnell
Last active September 10, 2021 18:12
Show Gist options
  • Save npodonnell/4a15d528f10598f01b3fbdf0db4e97ce to your computer and use it in GitHub Desktop.
Save npodonnell/4a15d528f10598f01b3fbdf0db4e97ce to your computer and use it in GitHub Desktop.
GNU Octave Cheatsheet

GNU Octave Cheatsheet

Math

Simple Math

>> 1+2
ans =  3
>> 3-1
ans =  2
>> 4*5
ans =  20
>> 5/2
ans =  2.5000
>> mod(10, 3)
ans =  1

More Advanced Math

>> abs(-2)
ans =  2
>> sin(pi)
ans =    1.2246e-16
>> cos(2*pi)
ans =  1
>> e
ans =  2.7183
>> log(e)
ans =  1
>> log10(1000)
ans =  3

Complex Numbers

>> c1=i
c1 =  0 + 1i
>> c2=1
c2 =  1
>> c1+c2
ans =  1 + 1i
>> e^(pi * i)
ans = -1.0000e+00 + 1.2246e-16i
>> exp(pi * i)
ans = -1.0000e+00 + 1.2246e-16i

Matrices

>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9]
A =

   1   2   3
   4   5   6
   7   8   9

>> B = [2, 3, 5; 7, 11, 13; 17, 19, 23]
B =

    2    3    5
    7   11   13
   17   19   23

>> A*B
ans =

    67    82   100
   145   181   223
   223   280   346

>> A' # A transpose
ans =
   1   4   7
   2   5   8
   3   6   9

Get Dimensions

>> columns(A)
ans =  3
>> rows(A)
ans =  3

Reshape Matrix

>> A = [1, 2, 3, 4, 5, 6, 7, 8, 9]
A =

   1   2   3   4   5   6   7   8   9

>> reshape(A, 3, 3)
ans =

   1   4   7
   2   5   8
   3   6   9

Vectors

Row Vector

>> v1 = [-1, 2, 4]
v1 =

  -1   2   4

>> norm(v1)
ans =  4.5826

Column Vector

>> v2 = [-1; 2; 4]
v2 =

  -1
   2
   4

Convolution

>> conv([1, 2, -2],[3, 4, -2])
ans =

    3   10    0  -12    4

Plotting

2 Vectors

>> x = [1, 2, 3, 4, 5]
x =

   1   2   3   4   5

>> y = [1, 4, 9, 16, 25]
y =

    1    4    9   16   25

>> plot(x,y)

Plot Sine of a Vector

>> x = [pi*0, pi*0.25, pi*0.5, pi*0.75, pi, pi*1.25, pi*1.5, pi*1.75, pi*2, pi*2.25, pi*2.5]
x =

 Columns 1 through 10:

   0.00000   0.78540   1.57080   2.35619   3.14159   3.92699   4.71239   5.49779   6.28319   7.06858

 Column 11:

   7.85398

>> plot(x, sin(x))

Smoother Sine Curve Using linspace

>> x = linspace(0, 2*pi, 12)
x =

 Columns 1 through 10:

   0.00000   0.57120   1.14240   1.71360   2.28479   2.85599   3.42719   3.99839   4.56959   5.14079

 Columns 11 and 12:

   5.71199   6.28319

>> plot(x,sin(x))

Add Labels

xlabel("something")
ylabel("something else")

Keep and Add to Current Plot

hold on

Add Legends

legend("sin", "cos")

Add Title

title("My Plots")

Create Image from Matrix A

imagesc(A)

Same Thing, but Grayscale

imagesc(A), colorbar, colormap gray

Get Help on Plotting

help plot

Audio

Get Information About Audio File

>> audioinfo("sounds/Carillion.wav")
ans =

  scalar structure containing the fields:

    Filename = Carillion.wav
    CompressionMethod =
    NumChannels =  1
    SampleRate =  44100
    TotalSamples =  619306
    Duration =  14.043
    BitsPerSample =  16
    BitRate = -1
    Title = 120LUTH3
    Artist =
    Comment =

Read Audio File

[y, fs] = audioread("sounds/Carillion.wav")

Play Audio

>> player = audioplayer(y,fs)
>> play(player)

General Programmer Stuff

Print

>> disp("one plus one is")
one plus one is
>> disp(1+1)
 2

Formatted Print

x =  5
>> printf("x is %d\n",x)
x is 5

If-Statement

>> if (1==1)
  disp("good")
endif
good

For-Loop

>> for i = [1,2,3]
  disp(i)
endfor

While-Loop

>> while true,
  disp("hi");
end;
hi
hi
hi
...

Undefining a Variable

>> clear x

Functions

No Arguments

>> function foo
  disp("hi")
endfunction
>> foo
hi

With Arguments

>> function add(a, b)
  disp(a+b)
endfunction
>> add(4, 5)
 9

With Arguments and Return Value

>> function sum = add(a, b)
  sum = a + b
endfunction
>> c = add(3, 4)
sum =  7
c =  7

Filesystem / File IO

Print Current Directory

pwd

Change Directory

cd '/home/neil/'

List Directory

ls '/home/neil'

Load a .dat File

load('somefile.dat')

Save Variable v to a File

save hello.mat v;

Save Variable v to a File (ASCII)

save hello.txt v -ascii;

Other Stuff

Supress Output

oldpager = PAGER('/dev/null');
oldpso = page_screen_output(1);
oldpoi = page_output_immediately(1);

% Call function here

PAGER(oldpager);
page_screen_output(oldpso);
page_output_immediately(oldpoi);

Show All Variables in Current Workspace

who
@MartinAramayo
Copy link

Great contribution, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment