Struggle with logical indexing
2 views (last 30 days)
Show older comments
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!
0 Comments
Answers (1)
Steven Lord
on 23 Feb 2022
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)
logicalIndices = A(:, 3) > 10
subscriptIndices = find(logicalIndices)
useLogicalIndices = A(logicalIndices, :)
useSubscriptIndices = A(subscriptIndices, :)
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)
With this you can write more descriptive code.
wasWrongAnswerWithHighConfidence = (~T.correct) & (T.confidence > 0.9)
wrongAnswers = T.answer(wasWrongAnswerWithHighConfidence) % or
subtable = T(wasWrongAnswerWithHighConfidence, :)
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!