English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In the MATLAB environment, each variable is an array or matrix.
You can allocate variables in a simple way. For example,
x = 3 % Define x and initialize it with a value
MATLAB will execute the above statement and return the following result-
x = 3
it creates a variable named x1multiply1matrix, and assign the value3stored in its elements. For example,
x = sqrt(16) % Define x and initialize it with an expression
MATLAB will execute the above statement and return the following result-
x = 4
Please note-
After entering the variable into the system, you can refer to it later.
A variable must have a value before it is used.
When the expression returns a result not assigned to any variable, the system will assign it to a variable named 'ans', which can be used later.
For example,
sqrt(78)
MATLAB will execute the above statement and return the following result-
ans = 8.8318
You can use this variableans-
sqrt(78); 9876/ans
MATLAB will execute the above statement and return the following result-
ans = 1118.2
Let's see another example-
x = 7 * 8; y = x * 7.89
MATLAB will execute the above statement and return the following result-
y = 441.84
You can perform multiple assignments on the same line. For example,
a = 2; b = 7; c = a * b
MATLAB will execute the above statement and return the following result-
c = 14
whocommand displays all variable names you have used.
who
MATLAB will execute the above statement and return the following result-
Your variables are: a ans b c
whoscommand displays more information about variables-
variables in memory currently
type of each variable
memory allocated to each variable
whether they are complex variables
whos
MATLAB will execute the above statement and return the following result-
Attr Name Size Bytes Class ==== ==== a 1x1 8 double ans 1x70 757 cell b 1x1 8 double c 1x1 8 double Total is 73 elements using 781 bytes
TheclearThe command variable(s) deletes all (or specified) variables from memory.
clear x % it will delete x, won't display anything clear % it will delete all variables in the workspace % peacefully and unobtrusively
You can use an ellipsis (...) to extend a long expression to another line. For example,
initial_velocity = 0; acceleration = 9.8; time = 20; final_velocity = initial_velocity + acceleration * time
MATLAB will execute the above statement and return the following result-
final_velocity = 196
By default, MATLAB displays numbers with four decimal places. This is known asshort format.
However, if you want to increase the precision, you need to useformatcommand.
format longThe command displays16digit places.
For example-
format long x = 7 + 10/3 + 5 ^ 1.2
MATLAB will execute the above statement and return the following result-
x = 17.2319816406394
Another example,
format short x = 7 + 10/3 + 5 ^ 1.2
MATLAB will execute the above statement and return the following result-
x = 17.232
format bankThe command rounds numbers to two decimal places. For example,
format bank daily_wage = 177.45; weekly_wage = daily_wage * 6
MATLAB will execute the above statement and return the following result-
weekly_wage = 1064.70
MATLAB uses exponential notation to display a large number of numbers.
format short eThe command allows displaying in exponential form with four decimal places plus the exponent.
For example,
format short e 4.678 * 4.9
MATLAB will execute the above statement and return the following result-
ans = 2.2922e+01
format long eThe command allows displaying in exponential form with four decimal places plus the exponent. For example,
format long e x = pi
MATLAB will execute the above statement and return the following result-
x = 3.141592653589793e+00
format ratThe command gives the closest rational expression obtained from the calculation. For example,
format rat 4.678 * 4.9
MATLAB will execute the above statement and return the following result-
ans = 34177/1491
Vectors are one-dimensional arrays of numbers. MATLAB allows the creation of two types of vectors-
Row vectors
Column vectors
Row vectors(Row vectors) are created by enclosing the set of elements in square brackets and separating elements with spaces or commas.
For example,
r = [7 8 9 10 11]
MATLAB will execute the above statement and return the following result-
r = 7 8 9 10 11
Another example,
r = [7 8 9 10 11] t = [2 3 4 5 6] res = r + t
MATLAB will execute the above statement and return the following result-
res = 9 11 13 15 17
Column vectors By enclosing the set of elements in square brackets and separating elements with a semicolon (;).
c = [7; 8; 9; 10; 11]
MATLAB will execute the above statement and return the following result-
c = 7 8 9 10 11
A matrix is a two-dimensional array of numbers.
In MATLAB, matrices are created by entering the order of elements in each row separated by spaces or commas, and the end of each row is separated by a semicolon. For example, let's create a3×3The matrix is-
m = [1 2 3; 4 5 6; 7 8 9]
MATLAB will execute the above statement and return the following result-
m = 1 2 3 4 5 6 7 8 9