You are on page 1of 3

PS1('WHAT YOU WANT'); //CHANGE CONSOLE ; //SEMICOLON SUPPRESSES THE OUTPUT disp(var_name) sprintf('2 decimals: %0.

2f',a)//c-style formatting pi //constant format long //presents all numbers long style format short // A= [ row space separated ; ...] matriz-mx1 or row vector =[ col space separated] col vector=[data;data;...] v=1:0.1:2 start:step:final sequence (like space separated) v=1:6 step is assumed to be zero special matrices ones(m,n) //matriz full of ones 2*ones(m,n) zeros(m,n) rand(1,3) //full of random numbers in 0..1 randn(m,n) //randon normalized functions sqrt(number) w=matrix hist(w) hist(w,50) //50 is the number of beams or bars eye(m) //identity matrix of mxm size help command-name //gets you help size(matrix) //returns matrix [m n] size(matrix,1)//returns m size(matrix,2)//returns n length(matrix)//returns size of longest dimension, //usually applied on vectors pwd//returns current working path cd 'directory address' ls //list contents on the current directory load filename.dat or load('filename.dat') who //shows the variables in the current environment whos //like who but with details clear varname //eliminates the variable vectorname(start:numberofelements)//returns vector of first //number of elements save filename.mat variablename;//to save into a file //saves it in binary format save hello.txt varname -ascii //to save as ascii clear //deletes all variable in the working space //return specific element ixj matrixname(i,j) a(2,:) //":" means every element along the row/column a([col1 col2],:) //eg a([1 3],:) a(:,2)=[2;2;3]//assign a column vector a=[a,[another matrix]]//append a matrix

a(:)//returns all elements of into a single column vector c=[matrix1 matrix2]//concatenates horizontally, comma between the matrices is possible c=[matrix1 matrix2]//concatenates vertically a*b //matrices multiplication a.*b //multiplication element by correspondent element a.^potencia //potencia de cada elemento 1./matriz //element reciprocal one by one log(matrix) exp(matrix) abs(matrix) matrix+1 //adds 1 to each element //also can use ones combinations a' //transpose of a max(matrix) //returns max element of the matrix [val, ind] =max(matrix) //returns max value and its index matrix<scalar //compares each element with scalar and returns 0 if false or 1 if true find(condition) //returns indexes //condition can be comparison magic(dimension)//rows, columns, diagonals sum up to the same //value [rows,col]=find(A>7) sum(matrix)//returns sum of all elements prod(matrix)//returns product of all values floor(matrix) ceil(matrix) max(max(matrix)) sum(matrix,1) sums by col sum(matrix,2) sums by row flipud(matrix) pinv(matrix) plotting data t=row vector y=also row vector plot(t,y) //plot x,y hold on; //allows to plot many graphs plot(t,y2,'r') //in red color xlabel('time') ylabel('value') legend('sin','cos') title('my plot') print -dpng 'myplot.png' close //closes plot window figure(1); plot(); figure(2);plot();

subplot(1,2,1); divides plot 1x2 grid access firs element plot() subplot(1,2,2);//access 2nd element plot() axis([minx maxx miny maxy]) clf; //clears figure imagesc(matrix) imagesc(matrix), colorbar, colormap gray; control statements for i=1:10, matrix(i)=2*i; end; while var<=5, v(i)=100; i=i+1; end; break; //also usable functions create file named and extension .m like: function y=squarethisnumber(x) y=x^2; //then load the function file go to pwd where the file is or modify the octave search path addpath('directory') function [y1,y2]=squareAndCubeThisNumber(x) y1=x^2; y2=x^3;//returns two values

You might also like