Struggle with logical indexing

3 次查看(过去 30 天)
Steve Schulz
Steve Schulz 2022-2-23
Hey,
I'm wokring on some data for a research project and I'm still pretty new to matlab. I would like to extract information from an array given specific conditions.
% Find incorrect answers (incorrect answer = 0) that were given with high confidence (high confidence = > 0.5)
c = find((temp_data_bundle.data_falt(:,21) > 0.5) & (temp_data_bundle.data_falt(:,13) == 0))
% Find the corresponding question
question = temp_data_bundle.data_falt(c,8)
% Find the run in which the answer was given (there are 6 runs in total)
run = temp_data_bundle.data_falt(c,3)
To explain it more clearly:
The participant gave an erroneous response (but with high confidence that the response is correct) regarding question 6. Each question (1-10) will be repeated 4 times. In this case the participant will have 3 chances left to correct his answer regarding question 6. Now I would like to extract these 3 trials that are left and their responses regarding that specific question.
I would be very pleased if someone could give me a hint. Have a nice day!

回答(1 个)

Steven Lord
Steven Lord 2022-2-23
You're not actually using logical indexing, you're using subscripted indexing. If you omitted the find call where you defined c you would be using logical indexing (in one of the dimensions.)
A = magic(5)
A = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
logicalIndices = A(:, 3) > 10
logicalIndices = 5×1 logical array
0 0 1 1 1
subscriptIndices = find(logicalIndices)
subscriptIndices = 3×1
3 4 5
useLogicalIndices = A(logicalIndices, :)
useLogicalIndices = 3×5
4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
useSubscriptIndices = A(subscriptIndices, :)
useSubscriptIndices = 3×5
4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
Given that clarification, I'm not quite sure what your question means, "Now I would like to extract these 3 trials that are left and their responses regarding that specific question." Perhaps if you showed a few representative rows of your data in temp_data_bundle.data_falt and explained the meanings of each column (do some of them represent the 'trials' aka chances for a user to answer a question?) we might be able to offer some guidance.
As a suggestion I might consider storing the data in a table array and giving each variable in the table a descriptive name, something like:
% Random / arbitrary sample data
questionNumber = [1; 1; 2; 3; 4];
trialNumber = [1; 2; 1; 1; 1];
rng default
answer = randi(4, 5, 1);
confidence = rand(5, 1);
correct = answer == 3;
% The table
T = table(questionNumber, trialNumber, answer, confidence, correct)
T = 5×5 table
questionNumber trialNumber answer confidence correct ______________ ___________ ______ __________ _______ 1 1 4 0.09754 false 1 2 4 0.2785 false 2 1 1 0.54688 false 3 1 4 0.95751 false 4 1 3 0.96489 true
With this you can write more descriptive code.
wasWrongAnswerWithHighConfidence = (~T.correct) & (T.confidence > 0.9)
wasWrongAnswerWithHighConfidence = 5×1 logical array
0 0 0 1 0
wrongAnswers = T.answer(wasWrongAnswerWithHighConfidence) % or
wrongAnswers = 4
subtable = T(wasWrongAnswerWithHighConfidence, :)
subtable = 1×5 table
questionNumber trialNumber answer confidence correct ______________ ___________ ______ __________ _______ 3 1 4 0.95751 false

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by