How to find specific elements in cell array not followed by another specific element?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Mandy B.
il 10 Dic 2022
Commentato: Mandy B.
il 16 Dic 2022
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 Commenti
Walter Roberson
il 10 Dic 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 ?
Risposta accettata
Walter Roberson
il 12 Dic 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);
Più risposte (1)
Peter Perkins
il 12 Dic 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)
action = tt.action(1:end-1);
reaction = tt.action(2:end);
badRedReaction = (action=="redSquare" & reaction~="redButtonPressed");
badBlueReaction = (action=="blueSquare" & reaction~="blueButtonPressed");
tt(badRedReaction|badBlueReaction,:)
Vedere anche
Categorie
Scopri di più su Matrices and Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!