Preparing for mathematical modeling 2 - MATLAB imports data and handles missing values

1. Import data 1

Step 1: Import the file.
The most commonly used method is to import excel table data. Item tab -> Import data -> Select the excel file .

The second step is to select the imported range.

●The range of imported data starts from the second row by default, and the first row is usually the title row.
■If you don’t want to import all the data, you can hold down the CTRI key and select the content you want to import, such as a certain row or column.
■The "variable name row" means that after importing, the variables will be displayed at the top of the table in matlab. Generally, the first row of the original file is selected by default. But only English can be recognized. If it is Chinese characters, it becomes VerName".
According to the options shown in the figure below, you can also know and change some things. The
Insert image description here
third step is to select the import type
Insert image description here
. The fourth step is that if there are values ​​that cannot be imported, use Replace with NAN (don’t know what it is) or how to remove it
Insert image description here

Note that the data is in the workspace after importing. The data disappears after closing matlab. If you want to save the data, remember to save the workspace file!

2. Import data 2

Place the file in the current running file directory and use the command: Import the mat file in the current directory

 X = load('matlab.mat')

Insert image description here
andImport XECEL file as matrixis another function:

A =xlsread('G.xlsx')  %导入矩阵

3. Handling missing values ​​and outliers

1. Clean up missing numbers

In the real-time editor , select Tasks -> Clean Missing Data.
First, write - a group containing examples of missing values ​​and outliers.

x =1:100; %构造一个数组(矩阵),元素为123...100

%构造数据值
%矩阵元素的均值为0,方差o^2 = 1,且是正态分布的随机数
data = randn(1, 100);  % randn(1, 100)意味着生成一个1100列的矩阵

%设置第20406080个元素为缺失值
data(20:20:80) = NaN;


% data的值都是在0附近的,设置4个异常值
data(10) = -50;
data(40) = 45;
data(70) = -40;
data(90) = 50;
p1ot(x, data)

Abnormal data operation results:
Insert image description here
The version is too low and data cannot be processed without a real-time editor.
Insert image description here
Insert image description here
Then it can be dealt with.

Guess you like

Origin blog.csdn.net/qq_52626583/article/details/126808507