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