Loading and Manipulate Data

Loading and Manipulate Data

Loading and Manipulating Data

#1 Select the …. no selecting … you’ll use the carbigdata set to explore
Information on this is located at ….
https://www.mathworks.com/help/stats/_bq9uxn4.html

#2 Write a Matlab script that ….
• Has your name and section number in the right format at the top
• Loads the data file in to Matlab
• Explore one of the variables in the data set
• Write to the command window using fprintfwrite the values of the mode, mean, max and min of the variable
• Repeat for another variable in the data set.

#2.5 Histograms
• Create a histogram to represent one of the variables
• Use Acceleration or Cylinders if you don’t know where to start
• Create a new figure window called “Bin Exploration”
• Create 4 plots in this window which explore the effects of bin size on the histogram
• Use help histogram to figure out how

#3 Use scatter and subplot …
• Open a new figure window – called Scatter Plots
• Create three scatter plots in this window which explore the relationships between three variables
• One of the scatter plots must be of two variables which (most likely) have no relationship
• for example: “sugar content” and “servings per box” in a dataset on breakfast cereals
• Or … “date of birth” and “exam grade”
• The other scatter plots are your choice

#4 Add appropriate titles and labels to each of the plots
#5 Submit the entire assignment as a single PUBLISHED pdf
Using Matlab, upload the attached exam grades (Excel file) for the sample class.

Determine each student’s average on the exams.

Use “if then” statements to assign overall grades for each student according to the following rubric.

0-59 = E   ;   60-69 =  D  ;   70 – 79 = C  ;   80 – 89 = B    ;  90 – 100 = A

Use the “display” command to print the output of the average grades and the letter grades to the command screen.

Publish the results as a PDF.

Solution 

%MDHomework_1 is a script for manipulating a data set

%

% Name:

% Section number:

% prepare

clear all

close all

clc

% #1: exploration dataset

loadcarbig                 % load carbig.mat dataset

% #2 pick a variable in the dataset (Acceleration in this case) and obtain

% the mode, mean, max and min and print the values in the command widnow.

% compute measures

A_meas = [mode(Acceleration) mean(Acceleration) max(Acceleration) …

min(Acceleration)];

% display

fprintf(‘\n\nVariable Chosen: Acceleration [m/s^2]\n’)

fprintf(‘_____________________________________________________________\n’)

fprintf(‘%12s%12s%12s%12s\n’,’mode’,’mean’,’maximum’,’minimum’)

fprintf(‘%12.4f%12.4f%12.4f%12.4f\n\n’,A_meas)

% #2.5: Histograms & #4 for titles and labeling

% effect of the number of bins

L = length(Acceleration);               % number of vehicles

N = [10 20 40 60];                      % number of points in a bin

nbins = fix(L./N);                      % vector of bin sizes

% create new figure

figure(‘Name’,’Bin Exploration’,’NumberTitle’,’off’)

% create subplots within the figure

for n = 1:length(N)

subplot(2,2,n)                      % Select one of the figure areas

histogram(Acceleration,nbins(n))    % craete histogram in it

xlabel(‘Acceleration’)              % put a label in the x-axis

ylabel(‘Nubmer of cars’)            % put a label in the y-axis

title([‘Bins = ‘ num2str(nbins(n))])% title

end

% #3: Scatter plots & #4 for titles and labeling

% Selectedvaraibles: Acceleration, Horsepower and MPG

ps = {‘Acceleration’,’Horsepower’,’MPG’};       % the variables

ds = [Acceleration Horsepower MPG];             % combined dataset

pc = [1 2; 3 1; 2 3];                           % plot combinations

% Create figure

figure(‘Name’,’Scatter Plots’,’NumberTitle’,’off’,’Position’,…

[403 54 560 612])

% Create subplots

for n = 1:length(ps)

subplot(3,1,n)                              % select subplot region

scatter(ds(:,pc(n,2)), ds(:,pc(n,1)), ‘.’)  % scatter plot

xlabel(ps{pc(n,2)})                         % x-axis label

ylabel(ps{pc(n,1)})                         % y-axis label

title([ps{pc(n,1)} ‘ vs ‘ ps{pc(n,2)}])     % title

end 

%MDHomework_2 is a script for manipulating a data set of exam grades

%

% Name:

% Section number:

% prepare

clear all

close all

clc

% read data from excel sheet

f_name = ‘2.xlsx’;                      % excel file name

[num, txt] = xlsread(f_name);           % read numerical and text data

% deternine each student’s average

s_avg = mean(num,2);

% assign grades

s_grade = cell(size(txt));                    % initialize grade cell array

% rubric

E = [ 0  59];

D = [60  69];

C = [70  79];

B = [80  89];

A = [90 100];

% assign grades according to performance

display(‘Averages and Grades:’)

for n = 1:length(txt)

x = round(s_avg(n));            % make the mean integer

if x>=E(1) && x<= E(2)

s_grade{n} = ‘E’;

elseif x>=D(1) && x<= D(2)

s_grade{n} = ‘D’;

elseif x>=C(1) && x<= C(2)

s_grade{n} = ‘C’;

elseif x>=B(1) && x<= B(2)

s_grade{n} = ‘B’;

elseif x>=A(1) && x<= A(2)

s_grade{n} = ‘A’;

else % not applicable

s_grade{n} = ‘NA’;

end

% display results

display([num2str(s_avg(n),’%5.2f’) ‘  ‘ s_grade{n}])

end