Extracting data from array

3 views (last 30 days)
Vinay Srinivasan
Vinay Srinivasan on 3 Jan 2020
Hello
This question seem to be simple,but I am new to Matlab so it is getting difficult for me.
I have a file named as "Event" which contain data from 100 car accident events. Each car accident event has several data like number of person sitting in the car,seveirty(crash or near crash),type of distraction(talking in phone by driver or disturbance from adjacent passenger) and many more. I want to count events where "Crash" has happened and the type of distaction is "talking on phone ". How to write a code for this ?.

Accepted Answer

Cameron B
Cameron B on 3 Jan 2020
%c is our array of data. column 1 is whether there was a crash, and column 2 is the reason
c = {'Crash';'Crash';'Nothing';'Crash';'Crash';'Crash';'Nothing';'Nothing';'Crash';'Nothing'};
c(:,2) = {'Alcohol','Talking on Phone','Sleep','Talking on Phone',...
'Talking on Phone','Distracted','Talking on Phone','Ice','Car Malfunction','Talking on Phone'};
NumInc=sum(strcmp('Talking on Phone',c(:,2)) & strcmp('Crash',c(:,1))); %the only real important bit
sprintf('The number of people who were in a car crash because of talking on the phone is %d',NumInc)
disp(c)
This will count if both conditions are satisfied. The conditions are that the person was in a crash and the cause was talking on the phone. I've displaced the cell array c at the end so you can count for yourself that it is correct.

More Answers (0)

Categories

Find more on MATLAB Mobile 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!