Basic Commands
Page 1 of 5
|| Sarvam Krishna Arpanam ||
Sl. No
Operator or Function Purpose
1 a = 5 Define a scalar without semi colon operator
2 b = 10; Define a scalar with a semi colon operator
3 c = a + b Basic addition operator
4 who Shows the variables in the workspace
5 whos Information about the variables in the workspace
6 clc Clearing the command window
7 clear all Clearing the workspace
8 A = [2 5 6 7] Defining a row vector 9 B = [9 11 14 15]; Defining a row vector with semicolon operator
10 C = A + B Vector addition; Try subtraction
11 D = B' Transpose of a vector
12 Multi = A*D Multiplication of two vectors
13 Multi_new = A.*B Multiplying each element of A with each element of B
14 div_new = A.\B Multiplying each element of A by each element of B
15 powA = A.^5 Every element of A is raised to the power of 5
16 greatest = max(A) Maximum of a vector; Try min(A) for minimum 17 tot = sum(A) Sum totaling the elements of a vector; try prod(A) 18 averageA = mean(A) Determining the mean of vector A; try median(A) and
mode(A) 19 std(A) Determining the standard deviation of A; try var(A) for
variance.
20 length(A) Determines the length of a vecotr 21 A = [2 3 4; 9 10 11; 17 8 19]
B = [8 9 0; 7 12 13; 21 45 6] Defining square matrices
22 A(2,3) Accessing the element at 2nd row and third column of A 23 A(:,2) Accessing all rows, 2nd column 24 A(1,:) Accessing, 1st row, all columns
Basic Commands
Page 2 of 5
25 C = A + B Addition of matrices; Try subtraction & multiplication
26 Cprod = A*B Product of two matrices
27 Element_prod = A.*B Dot Product
28 det(A) Gives the determinant of A 29 inv(A) Inverse of A 30 rank(A) Gives the rank of A 31 [m,n] = size(A) Determines the size of A matrix 32 eig(A) Determines the eigen values of A 33 eigvec(A) Determines the eigen vector of A 34 cond(A) Determines the condition number of A 35 input Enables user input
36 elfun Displays the elementary functions
37 abs(x), cos(x), exp(x), log(x),log10(x), cosh(x), sin(x), tan(x), sqrt(x), tanh(x)
Some of the built-in functions
38 4e4 Scientific form of 40000
39 pi, Inf, NaN, -Inf Inbuilt constants
40 display('Hello') Displays the string inside the quote. In this case, it displays Hello
41 mod(100,3) Displays Modulus after division 42 rand(5) Use of rand function to generate random numbers 43 randint(5) Use of randint function to generate random integers 44 randint(5,3,[291 108]) Randomly generate integers from a given range 45 D = zeros(5,3) Generate a matrix of zeros 46 E = ones(5,8) Generate a matrix of ones 47 eye(7) Generate an identity matrix of size 7 48 X = linspace(50,80,1000) Generate 1000 uniformly spaced points between 50 and
80
49 N = 7:18 Create elements from 7 to 18 with an interval of 1
50 N = 2: 0.5 : 5 Create elements from 2 to 5 with an interval of 0.5
Basic Commands
Page 3 of 5
51 ceil(3.1) Gives the ceil of a number 52 floor(4.95) Gives the floor of a number 53 round(6.45) Rounds the number to the nearest integer 54 A = randint(3,3,[10 100]);
C= diag(A) Extract the diagonal elements of A matrix
55 trace(A) Sum of the diagonal elements of A matrix 56 sum(diag(A)) Summing the diagonal elements of A matrix 57 max(A,[],1) Finds the maximum number in each column of A 58 max(A,[],2) Finds the maximum number in each row of A 59 A = 3+5i; Defining a single complex number with colon operator
60 B = 9+67i Defining a single complex number without colon operator
61 C = A+ B Addition of two complex numbers; try subtraction; Define matrices of complex numbers and multipy
62 tic MATLAB starts a stopwatch timer
63 toc MATLAB stops a stopwatch timer
64 save test Saves all the variables in the workspace to a MAT file titled test.MAT
65 save filename X Saves only the variable X in a MAT file titled filename
66 load test Loads all the variables in the file titled test.MAT to the workspace
67 what Shows the list of MATLAB specific files
68 dir Lists the contents of the current directory
69 pwd Shows the current directory
70 X = 1:10; Y = X.^2 plot(X,Y,'r*')
Plots a graph between the values of X and Y;
71 xlabel('Values of X') Adding label on the x-axis 72 ylabel('Values of Y') Adding label on the y-axis 73 title('Variation of X with Y') Adding a title to the plot 74 text(X(5),Y(5),'Central Adding a string on the plot at a specified point
Basic Commands
Page 4 of 5
Piont') 75 axis([4 8 10 40]) Plots the graph in between x = 4 to 8 and y = 10 to 40 76 grid Places grid on the plot
77 figure Creates a new figure (or plot) 78 plot(X,Y) Plots a graph between the X and Y values 79 close all Closes all the figure window
80 loglog Helps in the plotting of log log graph; Look into
semilogx and semilogy
81 X = 1:10; Y = X.^2;Z = X.^3
plot3(X,Y,Z) Plots a three dimensional plot with continuous line
82 hold on Preserves the contents of the current plot while plotting something new
83 clf Clears current figure
84 lookfor Helps to find help on a keyword (slow) 85 help Helps to find help on an existing command (fast) 86 demo Launches the demo window
87 str1 = 'Hi'
Str2 = 'How are you'
Defining strings
88 strvcat(str1,Str2) Does a vertical concatenation 89 strcat(str1,Str2) Does a horizontal concatenation 90 %This will not hurt Commenting
91 Recall previous lines 92 Recall next line 93 Mission = ['Will I be'...
'winning here'...
'or whining'];
Shows how a single command can be broken across multiple lines
94 a = [1 2 3;4 5 6;7 8 9] b = [a 10*a;-a [1 0 0;0 1 0;0 0 1]]
Shows concatenating of matrices
95 sort(x) Sort the elements of x in ascending order 96 unique(x) Extracts only the unique elements of x
Basic Commands
Page 5 of 5
Top Related