Skip to content

Instantly share code, notes, and snippets.

@juliuscanute
Last active October 12, 2019 21:30
Show Gist options
  • Save juliuscanute/b7e360f8c92fcc5a88c7214c733db9fa to your computer and use it in GitHub Desktop.
Save juliuscanute/b7e360f8c92fcc5a88c7214c733db9fa to your computer and use it in GitHub Desktop.
[Declare Variables] #matlab #math
//Declaring & Assigning variables
a = 1
b = 2
c = 3
//Clearing Variables
clear a b
clear
//Row vector
v1 = [ 1 2 3]
//Column vector
v2 = [ 1; 2; 3]
//Access vector element
v1(1)
//Vector uniformly spaced
v3 = 0:6
[0,1,2,3,4,5,6]
v4 = 0:0.5:6
[0,0.5,1.0....,6]
theta = -2*pi:pi/10:2*pi;
//Switch row to column and vice versa
v2 = v2'
v4 = (0:0.5:6)'
//2D Matrix
z = ones(3,4)
z =
1 1 1 1
1 1 1 1
1 1 1 1
z = ones(3,4)
z =
0 0 0
0 0 0
0 0 0
0 0 0
//Size
size(z)
ans = 4 3
size(theta)
ans = 1 41
//Allocate new vector
zeros(size(theta))
//Generate 20 normally distributed random numbers with a mean of zero and standard deviation of 1
randn(20,1)
z = y + 0.3*randn(size(theta));
//Plot
plot(y)
//Plots theta in the x axis & y in the y axis
plot(theta,y)
//Plot over an existing plot
hold on
plot(theta,z)
xlabel('Phase (radians)')
ylabel('Amplitude')
title('Ideal vs. Noisy Oscillations')
legend('Ideal','Noisy')
//Save variables in the workspace
save myData theta y z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment