MATLAB Variables

 

Each MATLAB variable can be an array or matrix.

Specifies that a variable with a simple method. E.g:

x = 3           % defining x and initializing it with a value

 

MATLAB code executes and returns the following results:

x =
     3

 

The above creates a 1-1 matrix storage values ​​called x and its elements. We can look at another example,

x = sqrt(16)     % defining x and initializing it with an expression

 

MATLAB code executes and returns the following results:

x =
     4

 

MATLAB Note:

    • Before using variables must be assigned.

    • When the system receives a variable that can be referenced.

E.g:

x = 7 * 8 ; 
and = x * 7.89

 

MATLAB will perform the above statement, and returns the following results:

y =
   441.8400

 

    • When the expression returns a result, not assigned to any variable, the system assigns to a variable named ans, the future can continue to use.

E.g:

sqrt(78)

% MATLAB will perform the above statement, and returns the following results:

ans = 
    8.8318

% Variable ans may continue to be used:

9876 / year

% MATLAB will perform the above statement, and returns the following results:

 

years = 
   1.1182e + 03

 

 

MATLAB can assign multiple

Multiple tasks can be in the same line. E.g,

a = 2; b = 7; c = a * b

% MATLAB will perform the above statement, and returns the following results:

c =
    14

 

 
  

How MATLAB display variable names that have been used?

You can use who command to display the names of all the variables that have been used in MATLAB.

the WHO 
% MATLAB will perform the above statement, and returns the following result: 

Your are the Variables: 
A ANS bcxy

 

whos command displays a little more about the variables:

  • The current variable memory

  • The type of each variable

  • Memory is assigned to each variable

  • Whether or not they are complex variables

whos
 % the MATLAB to perform the above statement, and returns the following results: 

  the Name Size Bytes Class the Attributes 

  A 1x1-                  . 8   Double               
  ANS 1x1-                  . 8   Double               
  B 1x1-                  . 8   Double               
  C 1x1-                  . 8   Double               
  X 1x1-                  . 8   Double               
  Y 1x1-                  . 8   Double

 

clear command to delete all (or designated) from memory variable (S).

clear x     % it will delete x, won't display anything
clear         % it will delete all variables in the workspace
             %  peacefully and unobtrusively 

 

Long task

Long task to another line can be extended by an ellipsis (...). E.g,

= initial_velocity 0 ; 
Acceleration = 9.8 ; 
Time = 20 is ; 
final_velocity = initial_velocity ...
     + * Acceleration Time
 % the MATLAB to perform the above statement, and returns the following results: 

final_velocity =
    196

 

MATLAB command format

By default, MATLAB four decimal places the value of the digital display. This is called the short format.

If you want to be more precise, we need to use the format command.

Long (long) command format display 16 after the decimal point.

E.g:

the format Long 
X = . 7 + 10 / . 3 + . 5 ^ 1.2 
% the MATLAB to perform the above statement, and returns the following: 

X =
   17.231981640639408

 

format short example:

the format Short 
X = . 7 + 10 / . 3 + . 5 ^ 1.2 
% the MATLAB to perform the above statement, and returns the following: 

X =
    17.2320

 

Whitespace formatting commands Round to two digits after the decimal point. E.g:

Bank the format 
daily_wage = 177.45 ; 
weekly_wage = daily_wage * . 6 
% the MATLAB to perform the above statement, and returns the following results: 

weekly_wage =
        1064.70

 

MATLAB displays extensive use of exponential notation.

Format short e command allows the display form of an exponential four decimal places, plus index.

E.g:

the format Short E
 4.678 * 4.9 
% the MATLAB to perform the above statement, and returns the following results: 

ANS =
    2.2922e + 01 

 

Long the format  E command allows display four decimal places as indexes, together with the index. E.g:

the format Long E 
X = PI
 % the MATLAB to perform the above statement, and returns the following: 

X =
      3.141592653589793e + 00

 

format rat  Rats given command format closest rational expressions, resulting from the calculation. E.g:

RAT the format
 4.678 * 4.9 
% the MATLAB to perform the above statement, and returns the following results: 

ANS =
    2063 / 90  

 

MATLAB create vector

Vector is the one-dimensional array of numbers. MATLAB allows you to create two types of vector:

  • Row vector

  • Column vector

Create a set enclosed in square brackets row vector elements, separated by a comma or space element.

E.g,

= R & lt [ . 7  . 8  . 9  10  . 11 ]
 % the MATLAB to perform the above statement, and returns the following results: 

R & lt = 
  the Columns . 1 through . 4 
       . 7               . 8               . 9              10        
  the Column . 5 
      . 11    

 

Another example,

R & lt = [ . 7  . 8  . 9  10  . 11 ]; 
T = [ 2 , . 3 , . 4 , . 5 , . 6 ]; 
RES = R & lt + T
 % the MATLAB to perform the above statement, and returns the following results: 

RES = 
  the Columns . 1 through . 4 
       . 9              . 11              13 is              15        
  the Column . 5 
      . 17

 

Partition elements; created column vector by the elements included in the square brackets, a semicolon ().

= C [ . 7 ;   . 8 ;   . 9 ;   10 ; . 11 ]
 % the MATLAB to perform the above statement, and returns the following results: 

C =
        . 7        
       . 8        
       . 9        
      10        
      . 11  

 

MATLAB create a matrix

Matrix is ​​a two-dimensional array of numbers.

In MATLAB, create a matrix element per line, or comma-delimited space sequence, the last row is designated by a semicolon.

For example, the following creates a matrix of 3 × 3:

= m [ . 1  2  . 3 ; . 4  . 5  . 6 ; . 7  . 8  . 9 ]
 % the MATLAB executes the statement, and return the following results: 

m =
        . 1               2               . 3        
       . 4               . 5               . 6        
       . 7               . 8               . 9

 



Reprinted URL: https: //www.w3cschool.cn/matlab/matlab-e61o28ge.html

Guess you like

Origin www.cnblogs.com/action0/p/12364867.html