Share E-Book

MATLAB Data Analysis (The MathWorks, Inc.) (z-library.sk, 1lib.sk, z-lib.sk)

Author

,

Rating No ratings yet

Log in to rate

Matlab
Language English

No Description

Format PDF
Size 2.2 MB
7
Views
(First 20 pages)

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Page 1
MATLAB® Data Analysis R2025a
Page 2
How to Contact MathWorks Latest news: www.mathworks.com Sales and services: www.mathworks.com/sales_and_services User community: www.mathworks.com/matlabcentral Technical support: www.mathworks.com/support/contact_us Phone: 508-647-7000 The MathWorks, Inc. 1 Apple Hill Drive Natick, MA 01760-2098 MATLAB® Data Analysis © COPYRIGHT 2005–2025 by The MathWorks, Inc. The software described in this document is furnished under a license agreement. The software may be used or copied only under the terms of the license agreement. No part of this manual may be photocopied or reproduced in any form without prior written consent from The MathWorks, Inc. FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by, for, or through the federal government of the United States. By accepting delivery of the Program or Documentation, the government hereby agrees that this software or documentation qualifies as commercial computer software or commercial computer software documentation as such terms are used or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and govern the use, modification, reproduction, release, performance, display, and disclosure of the Program and Documentation by the federal government (or other entity acquiring for or through the federal government) and shall supersede any conflicting contractual terms or conditions. If this License fails to meet the government's needs or is inconsistent in any respect with federal procurement law, the government agrees to return the Program and Documentation, unused, to The MathWorks, Inc. Trademarks MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders. Patents MathWorks products are protected by one or more U.S. patents. Please see www.mathworks.com/patents for more information.
Page 3
Revision History September 2005 Online only New for MATLAB 7.1 (Release 14SP3) March 2006 Online only Revised for MATLAB 7.2 (Release 2006a) September 2006 Online only Revised for MATLAB 7.3 (Release 2006b) March 2007 Online only Revised for MATLAB 7.4 (Release 2007a) September 2007 Online only Revised for MATLAB 7.5 (Release 2007b) March 2008 Online only Revised for MATLAB 7.6 (Release 2008a) October 2008 Online only Revised for MATLAB 7.7 (Release 2008b) March 2009 Online only Revised for MATLAB 7.8 (Release 2009a) September 2009 Online only Revised for MATLAB 7.9 (Release 2009b) March 2010 Online only Revised for MATLAB 7.10 (Release 2010a) September 2010 Online only Revised for MATLAB 7.11 (Release 2010b) April 2011 Online only Revised for MATLAB 7.12 (Release 2011a) September 2011 Online only Revised for MATLAB 7.13 (Release 2011b) March 2012 Online only Revised for MATLAB 7.14 (Release 2012a) September 2012 Online only Revised for MATLAB 8.0 (Release 2012b) March 2013 Online only Revised for MATLAB 8.1 (Release 2013a) September 2013 Online only Revised for MATLAB 8.2 (Release 2013b) March 2014 Online only Revised for MATLAB 8.3 (Release 2014a) October 2014 Online only Revised for MATLAB 8.4 (Release 2014b) March 2015 Online only Revised for MATLAB 8.5 (Release 2015a) September 2015 Online only Revised for MATLAB 8.6 (Release 2015b) March 2016 Online only Revised for MATLAB 9.0 (Release 2016a) September 2016 Online only Revised for MATLAB 9.1 (Release 2016b) March 2017 Online only Revised for MATLAB 9.2 (Release 2017a) September 2017 Online only Revised for MATLAB 9.3 (Release 2017b) March 2018 Online only Revised for MATLAB 9.4 (Release 2018a) September 2018 Online only Revised for MATLAB 9.5 (Release 2018b) March 2019 Online only Revised for MATLAB 9.6 (Release 2019a) September 2019 Online only Revised for MATLAB 9.7 (Release 2019b) March 2020 Online only Revised for MATLAB 9.8 (Release 2020a) September 2020 Online only Revised for MATLAB 9.9 (Release 2020b) March 2021 Online only Revised for MATLAB 9.10 (Release 2021a) September 2021 Online only Revised for MATLAB 9.11 (Release 2021b) March 2022 Online only Revised for MATLAB 9.12 (Release 2022a) September 2022 Online only Revised for MATLAB 9.13 (Release 2022b) March 2023 Online only Revised for MATLAB 9.14 (Release 2023a) September 2023 Online only Revised for Version 23.2 (R2023b) March 2024 Online only Revised for Version 24.1 (R2024a) September 2024 Online only Revised for Version 24.2 (R2024b) March 2025 Online only Revised for Version 25.1 (R2025a)
Page 4
(This page has no text content)
Page 5
Data Processing 1 Importing and Exporting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2 Importing Data into the Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2 Exporting Data from the Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2 Plotting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3 Load and Plot Data from Text File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3 Remove Linear Trends from Timetable Data . . . . . . . . . . . . . . . . . . . . . . . . 1-5 Missing Data in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8 Data Smoothing and Outlier Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-12 Summarize or Pivot Data in Tables Using Groups . . . . . . . . . . . . . . . . . . 1-23 Clean Messy Data and Locate Extrema Using Live Editor Tasks . . . . . . . 1-33 Filter Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-39 Filter Difference Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-39 Moving-Average Filter of Traffic Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-39 Modify Amplitude of Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-40 Smooth Data with Convolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-42 Computing with Descriptive Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-46 Functions for Calculating Descriptive Statistics . . . . . . . . . . . . . . . . . . . 1-46 Example: Using MATLAB Data Statistics . . . . . . . . . . . . . . . . . . . . . . . . . 1-47 Data Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-48 Identify and Visualize Correlated Variables . . . . . . . . . . . . . . . . . . . . . . . 1-55 Regression Analysis 2 Linear Correlation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2 Covariance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2 Correlation Coefficients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3 v Contents
Page 6
Linear Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5 Simple Linear Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5 Residuals and Goodness of Fit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8 Fitting Data with Curve Fitting Toolbox Functions . . . . . . . . . . . . . . . . . 2-11 Interactive Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13 Basic Fitting UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13 Preparing for Basic Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13 Opening the Basic Fitting UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13 Example: Using Basic Fitting UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-14 Programmatic Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-26 MATLAB Functions for Polynomial Models . . . . . . . . . . . . . . . . . . . . . . . 2-26 Linear Model with Nonpolynomial Terms . . . . . . . . . . . . . . . . . . . . . . . . 2-26 Multiple Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-27 Programmatic Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-28 Time Series Analysis 3 Time Series Objects and Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2 Manage Experiments 4 Keyboard Shortcuts for Experiment Manager . . . . . . . . . . . . . . . . . . . . . . . 4-2 Shortcuts for General Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 Shortcuts for Experiment Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 Shortcuts for Results Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-3 Run Experiments in Parallel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-4 Run Multiple Simultaneous Trials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-4 Run Single Trial on Multiple Workers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-4 Set Up Parallel Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-4 Offload Experiments as Batch Jobs to a Cluster . . . . . . . . . . . . . . . . . . . . . 4-6 Create Batch Job on Cluster . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6 Track Progress of Batch Job . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7 Cancel Batch Job . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-8 Delete Batch Job . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-8 Debug General-Purpose Experiments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-9 Start Debugging Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-9 Debug Experiment Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10 Verify Your Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10 Experiment with Predator-Prey Equations . . . . . . . . . . . . . . . . . . . . . . . . 4-11 vi Contents
Page 7
Compare Air Resistance Models for Projectile Motion . . . . . . . . . . . . . . 4-19 Convert MATLAB Code into Experiment . . . . . . . . . . . . . . . . . . . . . . . . . . 4-28 vii
Page 8
(This page has no text content)
Page 9
Data Processing • “Importing and Exporting Data” on page 1-2 • “Plotting Data” on page 1-3 • “Remove Linear Trends from Timetable Data” on page 1-5 • “Missing Data in MATLAB” on page 1-8 • “Data Smoothing and Outlier Detection” on page 1-12 • “Summarize or Pivot Data in Tables Using Groups” on page 1-23 • “Clean Messy Data and Locate Extrema Using Live Editor Tasks” on page 1-33 • “Filter Data” on page 1-39 • “Smooth Data with Convolution” on page 1-42 • “Computing with Descriptive Statistics” on page 1-46 • “Identify and Visualize Correlated Variables” on page 1-55 1
Page 10
Importing and Exporting Data In this section... “Importing Data into the Workspace” on page 1-2 “Exporting Data from the Workspace” on page 1-2 Importing Data into the Workspace The first step in analyzing data is to import it into the MATLAB workspace. See “Supported File Formats for Import and Export” for information about importing data from specific file formats. Exporting Data from the Workspace When you analyze your data, you might create new variables or modify imported variables. You can export variables from the MATLAB workspace to various file formats, both character-based and binary. You can, for example, create HDF and Microsoft® Excel® files containing your data. For details, see the documentation on “Supported File Formats for Import and Export”. 1 Data Processing 1-2
Page 11
Plotting Data In this section... “Introduction” on page 1-3 “Load and Plot Data from Text File” on page 1-3 Introduction After you import data into the MATLAB workspace, it is a good idea to plot the data so that you can explore its features. An exploratory plot of your data enables you to identify discontinuities and potential outliers, as well as the regions of interest. The MATLAB figure window displays plots. See “Types of MATLAB Plots” for a full description of the figure window. It also discusses the various interactive tools available for editing and customizing MATLAB graphics. Load and Plot Data from Text File This example uses sample data in count.dat, a space-delimited text file. The file consists of three sets of hourly traffic counts, recorded at three different town intersections over a 24-hour period. Each data column in the file represents data for one intersection. Load the count.dat Data Import data into the workspace using the load function. load count.dat Loading this data creates a 24-by-3 matrix called count in the MATLAB workspace. Get the size of the data matrix. [n,p] = size(count) n = 24 p = 3 n represents the number of rows, and p represents the number of columns. Plot the count.dat Data Create a time vector, t, containing integers from 1 to n. t = 1:n; Plot the data as a function of time, and annotate the plot. plot(t,count), legend('Location 1','Location 2','Location 3','Location','NorthWest') Plotting Data 1-3
Page 12
xlabel('Time'), ylabel('Vehicle Count') title('Traffic Counts at Three Intersections') See Also load | plot | legend | xlabel | ylabel | title | size More About • “Types of MATLAB Plots” 1 Data Processing 1-4
Page 13
Remove Linear Trends from Timetable Data This example shows how to remove a linear trend from daily closing stock prices in a timetable by using the detrend function. Alternatively, you can interactively find and remove trends from data using the Find and Remove Trends task in the Live Editor. If the data does have a trend, detrending forces the mean of the detrended data to zero and reduces overall variation. Create Stock Data Create a sample timetable containing daily closing stock prices. Use randomly sampled numbers from a normal distribution. x = 0:300; Time = days(x)'; dailyFluct = gallery("normaldata",size(x),2); closing = cumsum(dailyFluct) + 20 + x/100; StockPrice = closing'; TT = timetable(Time,StockPrice) TT=301×1 timetable Time StockPrice 0 days 21.749 1 day 21.892 2 days 22.227 3 days 21.443 4 days 21.768 5 days 21.251 6 days 22.193 7 days 23.368 8 days 21.332 9 days 20.698 10 days 22.449 11 days 22.946 12 days 24.004 13 days 25.503 14 days 26.783 15 days 24.937 ⋮ Plot and label the stock price data. plot(TT,"Time","StockPrice"); xlabel("Time (days)"); ylabel("Stock Price (dollars)"); Remove Linear Trends from Timetable Data 1-5
Page 14
Remove Trend Apply detrend, which performs a linear fit to the stock prices and removes the trend. Specify to append the detrended data to the input timetable. TT = detrend(TT,ReplaceValues=false); Compute the trend line by subtracting the detrended data from the input data. trend = TT.StockPrice - TT.StockPrice_detrended; TT = addvars(TT,trend,NewVariableNames="Trend") TT=301×3 timetable Time StockPrice StockPrice_detrended Trend 0 days 21.749 -3.7893 25.538 1 day 21.892 -3.7397 25.631 2 days 22.227 -3.4975 25.724 3 days 21.443 -4.3742 25.817 4 days 21.768 -4.1423 25.91 5 days 21.251 -4.7525 26.003 6 days 22.193 -3.9033 26.096 7 days 23.368 -2.8216 26.189 8 days 21.332 -4.9502 26.282 9 days 20.698 -5.6776 26.375 10 days 22.449 -4.0195 26.468 11 days 22.946 -3.6157 26.561 12 days 24.004 -2.6498 26.654 13 days 25.503 -1.2442 26.747 14 days 26.783 -0.056718 26.84 15 days 24.937 -1.9958 26.933 ⋮ 1 Data Processing 1-6
Page 15
Find the average closing price of the detrended data. average_detrended = mean(TT.StockPrice_detrended) average_detrended = 8.8287e-15 As expected, the detrended data has a mean very close to 0. Visualize Detrended Data Display the results by plotting the original daily closing stock prices, the trend line, the detrended data, and its mean. plot(TT,"StockPrice") hold on plot(TT,"Trend") plot(TT,"StockPrice_detrended") yline(average_detrended) legend("Input Data","Trend","Detrended Data", ... "Mean of Detrended Data","Location","northwest") xlabel("Time (days)"); ylabel("Stock Price (dollars)"); See Also Live Editor Tasks Find and Remove Trends Functions detrend | gallery | plot | cumsum Remove Linear Trends from Timetable Data 1-7
Page 16
Missing Data in MATLAB Working with missing data is a common task in data preprocessing. Although sometimes missing values signify a meaningful event in the data, they often represent unreliable or unusable data points. In either case, MATLAB® has many options for handling missing data. Create and Organize Missing Data The form that missing values take in MATLAB depends on the data type. For example, numeric data types such as double use NaN (not a number) to represent missing values. x = [NaN 1 2 3 4]; You can also use the missing value to represent missing numeric data or data of other types, such as datetime, string, and categorical. MATLAB automatically converts the missing value to the data's native type. xDouble = [missing 1 2 3 4] xDouble = 1×5 NaN 1 2 3 4 xDatetime = [missing datetime(2014,1:4,1)] xDatetime = 1×5 datetime NaT 01-Jan-2014 01-Feb-2014 01-Mar-2014 01-Apr-2014 xString = [missing "a" "b" "c" "d"] xString = 1×5 string <missing> "a" "b" "c" "d" xCategorical = [missing categorical({'cat1' 'cat2' 'cat3' 'cat4'})] xCategorical = 1×5 categorical <undefined> cat1 cat2 cat3 cat4 A data set might contain values that you want to treat as missing data, but are not standard MATLAB missing values in MATLAB such as NaN. You can use the standardizeMissing function to convert those values to the standard missing value for that data type. For example, treat 4 as a missing double value in addition to NaN. xStandard = standardizeMissing(xDouble,[4 NaN]) xStandard = 1×5 NaN 1 2 3 NaN Suppose you want to keep missing values as part of your data set but segregate them from the rest of the data. Several MATLAB functions enable you to control the placement of missing values before 1 Data Processing 1-8
Page 17
further processing. For example, use the 'MissingPlacement' option with the sort function to move NaNs to the end of the data. xSort = sort(xStandard,'MissingPlacement','last') xSort = 1×5 1 2 3 NaN NaN Find, Replace, and Ignore Missing Data Even if you do not explicitly create missing values in MATLAB, they can appear when importing existing data or computing with the data. If you are not aware of missing values in your data, subsequent computation or analysis can be misleading. For example, if you unknowingly plot a vector containing a NaN value, the NaN does not appear because the plot function ignores it and plots the remaining points normally. nanData = [1:9 NaN]; plot(1:10,nanData) However, if you compute the average of the data, the result is NaN. In this case, it is more helpful to know in advance that the data contains a NaN, and then choose to ignore or remove it before computing the average. meanData = mean(nanData) meanData = NaN One way to find NaNs in data is by using the isnan function, which returns a logical array indicating the location of any NaN value. Missing Data in MATLAB 1-9
Page 18
TF = isnan(nanData) TF = 1×10 logical array 0 0 0 0 0 0 0 0 0 1 Similarly, the ismissing function returns the location of missing values in data for multiple data types. TFdouble = ismissing(xDouble) TFdouble = 1×5 logical array 1 0 0 0 0 TFdatetime = ismissing(xDatetime) TFdatetime = 1×5 logical array 1 0 0 0 0 Suppose you are working with a table or timetable made up of variables with multiple data types. You can find all of the missing values with one call to ismissing, regardless of their type. xTable = table(xDouble',xDatetime',xString',xCategorical') xTable=5×4 table Var1 Var2 Var3 Var4 NaN NaT <missing> <undefined> 1 01-Jan-2014 "a" cat1 2 01-Feb-2014 "b" cat2 3 01-Mar-2014 "c" cat3 4 01-Apr-2014 "d" cat4 TF = ismissing(xTable) TF = 5×4 logical array 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Missing values can represent unusable data for processing or analysis. Use fillmissing to replace missing values with another value, or use rmmissing to remove missing values altogether. xFill = fillmissing(xStandard,'constant',0) xFill = 1×5 0 1 2 3 0 1 Data Processing 1-10
Page 19
xRemove = rmmissing(xStandard) xRemove = 1×3 1 2 3 Many MATLAB functions enable you to ignore missing values, without having to explicitly locate, fill, or remove them first. For example, if you compute the sum of a vector containing NaN values, the result is NaN. However, you can directly ignore NaNs in the sum by using the 'omitnan' option with the sum function. sumNan = sum(xDouble) sumNan = NaN sumOmitnan = sum(xDouble,'omitnan') sumOmitnan = 10 See Also ismissing | fillmissing | standardizeMissing | missing Related Examples • Clean Messy Data and Locate Extrema Using Live Editor Tasks on page 1-33 • “Clean Messy and Missing Data in Tables” Missing Data in MATLAB 1-11
Page 20
Data Smoothing and Outlier Detection This example shows how to use data smoothing to eliminate unwanted noise or behaviors in data using the smoothdata function. This example also shows how to identify and handle outliers, which are data points that are significantly different from the rest of the data, using the isoutlier, filloutliers, or rmoutliers functions. Alternatively, you can interactively smooth noisy data and handle outliers in data using the Smooth Data and Clean Outlier Data tasks in the Live Editor. Moving Window Methods Moving window methods are ways to process data in smaller batches at a time, typically in order to statistically represent a neighborhood of points in the data. The moving average is a common data smoothing technique that slides a window along the data, computing the mean of the points inside of each window. This can help to eliminate insignificant variations from one data point to the next. For example, consider wind speed measurements taken every minute for about 3 hours. Use the movmean function with a window size of 5 minutes to smooth out high-speed wind gusts. load windData.mat mins = 1:length(speed); window = 5; meanspeed = movmean(speed,window); plot(mins,speed,mins,meanspeed) axis tight legend("Measured Wind Speed","Average Wind Speed over 5 min Window") xlabel("Time") ylabel("Speed") Similarly, you can compute the median wind speed over a sliding window using the movmedian function. 1 Data Processing 1-12
The above is a preview of the first 20 pages. Register to read the complete e-book.

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay
← Back to List