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

1 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2022-12-10
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. 2022-12-11
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)!

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2022-12-12
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);

更多回答(1 个)

Peter Perkins
Peter Perkins 2022-12-12
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

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by