Extracting data from array

조회 수: 1 (최근 30일)
Vinay Srinivasan
Vinay Srinivasan 2020년 1월 3일
댓글: Vinay Srinivasan 2020년 1월 4일
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에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by