Determine driving styles of a driver
Show older comments

ZERO = 0
ECONOMIC = 1
NORMAL = 2
AGGRESSIVE = 3
ds_mapping= [ZERO,ECONOMIC,NORMAL,AGGRESSIVE]
for r=0:length(Acceleration)
if(abs(Acceleration(r))<=0.7),
fprintf('\n\tAcceleration = %5.2f m/sec^2:\t Driving Style: ZERO\tCLASS: %d',Acceleration(r),ds_mapping(0))
end
for r=1:length(Acceleration)
if(abs(Acceleration(r))>=0.7),
fprintf('\n\tAcceleration = %5.2f m/sec^2:\t Driving Style: ECONOMIC\tCLASS: %d',Acceleration(r),ds_mapping(1))
end
if(abs(Acceleration(r))>2.79 && Acceleration (r) <= 3.30),
fprintf('\n\tAcceleration = %5.2f m/sec^2:\t Driving Style: NORMAL:\t\tCLASS: %d',Acceleration(r),ds_mapping(2))
end
if(abs(Acceleration (r))>3.63),
fprintf('\n\tAcceleration = %5.2f m/sec^2:\t Driving Style: AGGRESSIVE:\tCLASS: %d', Acceleration(r),ds_mapping(3))
end
end
fprintf('\n\n')
8 Comments
Sam Chak
on 26 Apr 2022
Steven Lord
on 26 Apr 2022
Okay, we have the problem statement and the code you've written to solve the problem. Please tell us what questions you'd like answered about this code or what difficulties you're experiencing.
At first glance, you do have a problem in your code.
for r=0:length(Acceleration)
if(abs(Acceleration(r))<=0.7),
There's no such thing as element 0, row 0, column 0, etc. of an array in MATLAB. The first element / row / column / page / etc. of an array in MATLAB is 1. So your code will throw an error when it asks for Acceleration(0).
Ben Morgan
on 27 Apr 2022
Ben Morgan
on 27 Apr 2022
Ankit
on 27 Apr 2022
What does driving style ZERO corresponds to? In the problem statement there are three defined driving style.
Ankit
on 27 Apr 2022
In MATLAb indices should start from 1 instead of 0.
change ds_mapping(0) to ds_mapping(1) in your code
ZERO = 0
ECONOMIC = 1
NORMAL = 2
AGGRESSIVE = 3
ds_mapping= [ZERO,ECONOMIC,NORMAL,AGGRESSIVE]
for r=1:length(n)
if(abs(Acceleration(r))<=0.7),
fprintf('\n\tAcceleration = %5.2f m/sec^2:\t Driving Style: ZERO\tCLASS: %d',n(r),ds_mapping(1))
end
if(abs(Acceleration(r))>=0.7),
fprintf('\n\tAcceleration = %5.2f m/sec^2:\t Driving Style: ECONOMIC\tCLASS: %d',n(r),ds_mapping(2))
end
if(abs(Acceleration(r))>2.79 && Acceleration (r) <= 3.30),
fprintf('\n\tAcceleration = %5.2f m/sec^2:\t Driving Style: NORMAL:\t\tCLASS: %d',n(r),ds_mapping(3))
end
if(abs(Acceleration (r))>3.63),
fprintf('\n\tAcceleration = %5.2f m/sec^2:\t Driving Style: AGGRESSIVE:\tCLASS: %d', n(r),ds_mapping(4))
end
end
fprintf('\n\n')
end
end
Ben Morgan
on 27 Apr 2022
Ben Morgan
on 27 Apr 2022
Answers (1)
Hi Ben,
As per my understanding, you want to classify the driving system based on the acceleration and the mapping table shared and are facing issues while doing so.
You can achieve the same using the code below, where I'm populating the "ds_mapping" based on the mapping table and then creating a line plot with two different variables on both y-axes of the plot. Please not, as the actual acceleration array was not present, so I have created a random acceleration array.
a = -6.5;
b = 6.5;
timeInterval = 1800;
acceleration = a + (b-a) * rand(1,timeInterval);
ds_mapping = [];
for i=1:timeInterval
if(abs(acceleration(i))<0.7)
ds_mapping = [ds_mapping,0];
elseif(abs(acceleration(i))>=0.7 && abs(acceleration(i))<2.79)
ds_mapping = [ds_mapping,1];
elseif(abs(acceleration(i))>=2.79 && abs(acceleration(i))<3.64)
ds_mapping = [ds_mapping,2];
elseif(abs(acceleration(i))>=3.64)
ds_mapping = [ds_mapping,3];
end
end
figure;
hold on;
title("Acceleration and Driving Style classification");
xlabel("Time(s)");
yyaxis left;
plot(1:timeInterval,acceleration,"Color","blue");
ylabel("Acceleration(ms^2)");
ylim([-6.5 6.5]);
yyaxis right;
plot(1:timeInterval,ds_mapping,"Color","cyan");
ylabel("Driving Style");
ylim([0 3]);
hold off;
I hope it helps!
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
