Extracting data from array

1 次查看(过去 30 天)
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 ?.

采纳的回答

Cameron B
Cameron B 2020-1-3
%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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Mobile 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by