Matlab basic knowledge (operation interface path search variable matrix representation) exercise: convert Fahrenheit to Celsius, the program will run until you press the Enter key when you want to enter Fahrenheit and exit the program

One, Matlab download resources and installation steps

> See resource sharing for installation package and installation steps

Two, Matlab operating interface composition

1. Main window:
functional area + quick access toolbar + current folder toolbar
2. Command line window:
used to enter commands and display the execution results of commands.
3. Current folder window:
used to display the location of the currently opened file
4. Work area window (also called workspace window): The
work space is the memory space used to store various variables and results.

Three, Matlab function execution path search

When entering a command object in MATLAB, MATLAB first checks whether it is an existing variable, if not, then checks whether it is an internal function, if not, checks whether it is a program file, first checks whether it is the current folder Then check whether it is a program file in the file search path folder. So when naming variables, try not to have the same names as internal functions.

Three, variables and related operations

  1. Variables are essentially an abstraction of memory units.
  2. In MATLAB, variable names start with a letter, followed by a sequence of letters, numbers, or underscores, up to 63 characters.

Exercise error 1: Undefined functions or variables appear when writing the first .m file, because the file naming does not follow the rules, and it can run normally after correction.

  1. In MATLAB, variable names are case sensitive.

  2. The standard function names and command names provided by MATLAB must use lowercase letters.

  3. Pre-defined variables
    Pre-defined variables are variables defined by the system itself. For example, ans is the default assignment variable; i and j represent imaginary units; pi represents the pi.

  4. The who and whos commands are used to display a list of variable names that have been resident in the workspace in the command line window

  5. Memory variable file The file
    used to save the variables in the MATLAB workspace is called the memory variable file, and its extension is .mat, also called MAT file. It is a binary format file.
    (1) Store the data in the workspace to a file

>> a = magic(4);
>> save mydata1.mat

(2) Put the data in the file into the workspace

load('mydata1.mat','-ascii')

(3) Read the data in excl

Score = xlsread('111.xlsx')

Four, the matrix representation of Matlab's matrix

1. []: Brackets represent the matrix
Row: a = [1 2 3]
Column: b = [1;2;3]
2. ()indexing: Means to look for elements in the matrix

3. Colon operator
A = [1:100] Arithmetic 1 sequence
A = [1:2:100] Arithmetic 2 sequence
A = (3,:) means to select all the third row
A = (:, j) all the elements of matrix a j-th column of
a (i: i + m, j: j + n) matrix a of the I I +, and the j-m row all elements j + n columns of
a ( i:i+m, :) All elements in the i~i+m row of matrix A

4, the matrix of four operations:
: normal matrix multiplication
.
: Multiplying the corresponding element
/: INV * is approximately equal to A (B)
./: dividing the corresponding elements of equal
: A + a matrix plus real numbers, plus the corresponding element of A
^: relative Multiply by n A.
^: n-th power of the element at the corresponding position
': transpose

5. Some special matrices:
eye(n): The diagonal is 1, and the rest is 0.
Zeros(n1,n2): All 0
ones: All 1
diag: Diagonal elements

6. Related functions:
max(A): gives the maximum value of each column
max(max(A)): the maximum value of the matrix
min sum mean The same is true for
sort: sort (each column is sorted from largest to smallest, row to row Not tied together)
sortrows: sort by the size of the first column, row synchronization

7. Change the matrix shape
reshape(A,m,n) Under the premise that the total elements of the matrix remain unchanged, rearrange the matrix A into an m×n two-dimensional matrix.

5. A related exercise:

Topic: Convert Fahrenheit to Celsius, and the program will run until you press the Enter key when you want to enter Fahrenheit and exit the program

function  F2C

F = 1;
while F >0 
prompt = 'Temperature in F is : ';
F = input(prompt);
TF = isempty(F);
if TF == 1
    break;
else
C = (F-32)*5/9;
S = num2str(C);
E = ' ==> Temperature in C is :';
D = [E,S];
disp(D);
end
end

result:

>> F2C
Temperature in F is : 100
 ==> Temperature in C is :37.7778
Temperature in F is : 50
 ==> Temperature in C is :10
Temperature in F is : 
>> 

Guess you like

Origin blog.csdn.net/qq_42005540/article/details/108763195