Extracting data from array

1 visualización (últimos 30 días)
Vinay Srinivasan
Vinay Srinivasan el 3 de En. de 2020
Comentada: Vinay Srinivasan el 4 de En. de 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 ?.

Respuesta aceptada

Cameron B
Cameron B el 3 de En. de 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.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by