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 ?.
댓글 수: 0
채택된 답변
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개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!