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
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 ?
채택된 답변
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
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)
action = tt.action(1:end-1);
reaction = tt.action(2:end);
badRedReaction = (action=="redSquare" & reaction~="redButtonPressed");
badBlueReaction = (action=="blueSquare" & reaction~="blueButtonPressed");
tt(badRedReaction|badBlueReaction,:)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!