How to find specific elements in cell array not followed by another specific element?

1 visualización (últimos 30 días)
I have a cell array with the first column being events (redSquare, blueSquare, redButtonPressed, blueButtonPressed). I need to find the rows where redSquare is not followed by redButtonPressed and blueSquare is not followed by blueButtonPressed. I'm not really sure how to approach the problem without the logical NOT operator since it only works on numerical values (at least that's my understanding/experience). I would be extremely grateful or any help/direction. Thank you in advance!
  2 comentarios
Walter Roberson
Walter Roberson el 10 de Dic. de 2022
What is class() of those? For example is it a cell array of character vectors? Is it a cell array of categoricals?
Is the entire cell array consisting of the same kind of things, such that you could categorical() the entire cell array, after which you could work with rows and columns of categoricals ?
Mandy B.
Mandy B. el 11 de Dic. de 2022
The first column with my events is a character vector, and then the other columns are of class double and contain times. There's many other events in my data, I'm just only looking at a few of them. Will using categorical still work in that scenario?
Thank you for your help also, I apologize if my questions are a bit dumb (I'm still learning)!

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 12 de Dic. de 2022
Assuming you used readtable() then
Events = YourTable{:,1};
G = findgroups(Events);
%The ids will be in sorted order.. provided you can be certain there are no
%other ids in the data. Otherwise you really should check the second output
%of findgroups()
G_blueButtonPressed = 1;
G_blueSquare = 2;
G_redButtonPressed = 3;
G_redSquare = 4;
mask = (G(1:end-1) == G_redSquare & G(2:end) ~= G_redButtonPressed)) | ...
(G(1:end-1) == G_blueSquare & G(2:end) ~= G_blueButtonPressed));
row_idx = find(mask);

Más respuestas (1)

Peter Perkins
Peter Perkins el 12 de Dic. de 2022
If you have time and other data, think timetables (also Walter is right about categorical):
action = categorical([1;3;2;4;1;3;2;2;1;1],1:4,["redSquare" "blueSquare" "redButtonPressed" "blueButtonPressed"]);
value = rand(size(action));
time = seconds(1:length(action))';
tt = timetable(time,action,value)
tt = 10×2 timetable
time action value ______ _________________ ________ 1 sec redSquare 0.40283 2 sec redButtonPressed 0.58469 3 sec blueSquare 0.25583 4 sec blueButtonPressed 0.21057 5 sec redSquare 0.68091 6 sec redButtonPressed 0.84098 7 sec blueSquare 0.002893 8 sec blueSquare 0.57486 9 sec redSquare 0.58551 10 sec redSquare 0.60419
action = tt.action(1:end-1);
reaction = tt.action(2:end);
badRedReaction = (action=="redSquare" & reaction~="redButtonPressed");
badBlueReaction = (action=="blueSquare" & reaction~="blueButtonPressed");
tt(badRedReaction|badBlueReaction,:)
ans = 3×2 timetable
time action value _____ __________ ________ 7 sec blueSquare 0.002893 8 sec blueSquare 0.57486 9 sec redSquare 0.58551

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by