This chapter begins with a brief discussion about basic statistical functions used in MATLAB. After that, sorting, searching, and processing data with Microsoft Excel files are presented.
Basic Statistics
Some of the Basic Functions Related to Statistics in MATLAB
Function | Explanation | Example |
---|---|---|
max() | Returns the maximum element | max([1,34,21,5]) |
mean() | Returns the average or mean value | mean([1,34,21,5]) |
median() | Returns the median value | median([1,34,21,5]) |
min() | Returns the smallest value | min([1,34,21,5]) |
mode() | Returns the most frequently occurring value | mode([1,34,5,21,5]) |
std() | Returns the standard deviation | std([1,34,5,21,5]) |
var() | Returns the variance | var([1,34,5,21,5]) |
Example 6-1. Create a 3 × 4 matrix randomly by using the function randi(). Then, calculate the maximum value, mean value, and standard deviation of each column of the matrix.
Solution 6-1. The following code can be used to create the matrix and find the desired values for each column of the matrix.
Data Analysis
This section presents various sorting and searching functions, along with data processing examples.
Sorting and Searching
Sorting in ascending or descending order can easily be achieved via the sort function .
Example 6-2. Consider the data given by 23, 70, 86, 5, 120, 50, -9, 61, and 100. Write a code to sort the given values both in ascending and descending order.
Solution 6-2. The data can be introduced as a vector, and then can be sorted as shown here at the prompt.
In some cases, multiple sets of data, as in a matrix, need to be sorted. This case is shown in Example 6-3.
Example 6-3. Randomly create a 3 × 5 matrix by using the randi() function. Then, sort each column and each row of the matrix in descending order.
Solution 6-3. The following code can be used to sort the columns and rows of the matrix.
As shown in the output, if the command sort is used in the form sort(Matrix,1), sorting is performed for columns; if sort(Matrix,2) is used, then sorting is performed for rows.
Searching is also a straightforward task with MATLAB. Typically, there are three different possibilities available for searching. A number can be searched in a vector or matrix. A group of letters or a string can be searched in a string cluster. Finally, we can search some information within a dataset. We will examine each of these cases with an example.
Example 6-4. Consider the matrix given by Mat = [32,98,17,71,67; 39,71,65,28,17; 77,49,71,68,71]. Write code that searches for 71 within this matrix.
Solution 6-4. The following code can be written to perform the search.
As shown, MATLAB returns the index values where the number 71 occupies a place. The fifth, ninth, tenth, and fifteenth elements of the matrix have a value of 71. From this example, we see that the order of indexing goes column by column.
Example 6-5. Consider the string given by 12345 ABcde Antonio. Write code that looks for the string antonio without considering case-sensitivity.
Solution 6-5. MATLAB is a case-sensitive language. Therefore, the strings Antonio and antonio are two different strings. The following code can be used to search for the desired string.
This output states that the searched string is embedded in the bigger string, and it starts from the thirteenth element of the vector. If the string did not exist, then we would have gotten an empty set [], meaning that the searched word could not be found.
Let us load these data into the workspace. Then, search the dataset to find the last name DIAZ, and other relevant information. After that, find the people who are 50 years old.
Solution 6-6. To find DIAZ, we need to search the column LastName, which contains strings. To search for the number 50, we need to work with the column Age. The following code can be used to perform the given tasks.
In this code, the command ismember determines whether DIAZ is a member of the cell hospital.LastName or not. It returns 1 if it is the case, and 0 if it is not. By using the find command , it is possible to find out the index of the required information.
Data Processing
This section presents how to pull out and process information from a Microsoft Excel data file via an example.
Acquire the data from the second, third, and fourth columns, including their corresponding titles. Plot these data in a bar graph.
Solution 6-7. The following code can be used to accomplish the assigned task.
Here, we can get the column titles from the variables NewVar1, NewVar2, and NewVar3.
In MATLAB, you can work with comma-separated value (.csv) files as well.
Example 6-8. Write code to print the first five rows of the data from the outages.csv file from MATLAB.
Solution 6-8. The following code can be used to accomplish the given task.
Problems
6.1. Create a 4 × 5 matrix randomly by using the rand() function. Then, calculate the maximum value, mean value, and standard deviation for each column of the matrix.
6.2. Consider the data 30, 45, 100, 65, 98, 45, 61, and 10. Write a code to sort the given data both in ascending and descending order.
6.3. Randomly create a 2 × 6 matrix by using the randi() function. Then, sort each column and each row of the matrix in descending order.
6.4. Consider the matrix given by Mat = [41, 45, 100, 65, 41, 45, 61, 10]. Write code that searches for 41 within this matrix.
6.5. Consider the string given by 5361 Sen Antonio Ben Banderas. Write code that looks for the string anderas without considering case-sensitivity.