You are on page 1of 5

clear

size (x) size of a matrix

v= [1,2,3]; / v=[1 2 3 ] making a vector

semicolon: suppress output

a = [1 2 ; 3 4 ] matrix

v=v' making transpose

a(2,2)

v(2,1) selectig a specific number in that position

a(1,:) return all values in that row

a(:,1) return all values in that column

c(2:3, 2:3) return from range 2nd row 3rd column to 2rd column 3rd row

1:10 show numbers from 1 to 10

w= 1:10 assign the values to w as a row matrix

a.*b element by element multiplication

a./b

dot(x,y) 1 by 1 scaler product of functions

cross(x,y) cross product x*y*sin0

sin(0)

cos(0)

tan(0)

sin(a)

exp(0)

exp(1)

log(8)

sqrt(5)

pi if assigned as some variable cannot be used as normal constant pi

no built in variable e, so e =exp(1)

i constant = sqrt(-1)

eye(3)= 3*3 diagonal matrix


zeros(3) 3*3 zero matrix

zeros(3,2)

ones(2,3)

function y = my_sinh(x) file name and function name should be the same

y=(exp(x)-exp(-x))/2;

end

use % sign to make a comment

can use matrixes as input whise using function

ex: x= 0:10;

sinh(x)

mod (2,2) returns remainder

~ logial opposite

inv(a) inverse of a matrix

a\b =inv(a)*b more efficient way of calculation 3 linear systems with 3 variables

det(A) determinant of matrix A

e=eig(A) eigenvalue of a matrix

[V,D]= eig(A)

[V,D,W]= eig(A)

norm(A*V-V*D) distance from the origin point sqrt(square of all values)

for i =[1 3 5 6 2 9] sets i=1 for first iteretation, i=3 for 2nd, i=5 fpr 3rd…

disp(i); display index “i”

end

E= randn(1000,1) array of size 1000 to 1 of random numbers

For loops are inefficient in matlab so try to avoid

tic … toc show elapsed time during code execution


whos see all the variables in the workspace ad some specs

class (something) to see type of data

my_struct.name = ‘my new name’ can also add more data of different types

isfield ( my_struct, name) to check weather a struct has this field: returns 1 or 0

rmfield(my_struct, age) to remove a field from struct

setfield(mystruct, ‘gender’,’ f ’) to add field dynamically while struct is in use

(a struct can contain a struct)

my_struct.contact.phone =123456789

S = struct( ‘name’,’Bob’,’email’, ‘fafa@sgsşl.com’) to enter multiple values in a struct.. 1st parameter field.. 2d
parameter value and so on

my_cell{1}=’hello world’ kind of like struct but is like an array of things.. struct is like a group of
info

can also enter matrix to cell

y= [1,1,2,3,5,8,13,21]

plot(y) to plot the values of an array

x=[0.1,0.2,0.3,0.4,0.4,0.5,0.6,0.7]

plot(x,y) to plot the binary values !!condition:size of x and y HAVE to be equal

x=linspace(1,100,200) to automatically fill in the values of x: 1st beginning value, 2nd end value, total
number of values

plot(x,y,x2,y2)

plot(x,y,’.’,x2,y2,’—‘)

openfig(‘file.fig’)

bar(x) barchart

hist(x) make histogram of data

hist (x,50)

pie(x) make pie chart of x

scatter(x,y) plot individual data points

help xlsread documentation for xls function… not perfect

csv= comma separated value

csvread
B= eye(4)*5 4 by 4 diagonal matrix multiplied by 5

csvwrite(‘another.csv’,B) create a csv file

save(‘my_workspace.mat’) saves your whole workspace

load(‘my_workspace.mat’)

save(‘AC.mat’,’A’,’C’) save specific variables not all

subplot(2,1,1) number of rows, columns which subplot to plot

plot(X,Y)

subplot(2,1,2) stacks plot over each other instead of overlaying

plot(X,Z)

……………

Z= rand(100,100)

size(Z)

surf(Z) for 3d plotting

contour(Z) 2d plotting ..red=very high values.. blue= very low values

title(‘A plot of sinx’) puts title to the plot, can also be used for subplots.. same command twice

xlabel(‘smthng smthng’) label for the x axis

ylabel(‘for the y-axis’)

wavread(‘helloworld.wav’)/ audioread(‘helloworld.wav’) …wav=raw sound file

d= wavread(‘helloworld.wav’)

plot(d)

size(d)

fs= frequency sample

[d,fs]=wavread(‘falanfilan.wav’); read the data and store it

sound(d,fs) play audio

[d,fs]= audioread(‘hello.mp3’)

sound(d,fs)

d2= flipud(d) flid the data.. audio signal

plot(d2)
audiowrite(‘helloworld_reverse.mp4,d2,fs’) to create an audio file.. name, data, sample rates,, doesn’t
support mp3

d4= downsample(d,2)

size(d4)

sound(d4,fs)

sound(d4,fs/2) slow the speed by 2 times

fourier function: to transform the signal elements from time domain to frequency domain and then
reverse fourier to do vice versa

discrete fourier transform

Y=fft(y) fast fourier transform

plot(y)

real(2+3*i) drop the imaginary number

plot(real(y))

y2=(x.*x) element by element multiplication

spectrogram(y2) change frequency as time goes on

spectrogram(y2,500) 500.. window size of sampling

b=ones(40,1)/40;

dlp=filter(b,1,d) lowpass filter

sound(d,fs)

sound(dlp,fs)

A=imread(‘lena.bmp’)

Size(A)

Imshow(A)

You might also like