MATLAB Answers

How would I be able to output ID's that satisfied an if statement?

1 view (last 30 days)
Naruto
Naruto on 11 Nov 2020
Edited: dpb on 11 Nov 2020
How would I be able to output the student ID's that satisfied the if statement? And after this, how can I show it as a list?
for i = 1:1:num_rows;
for j = 1:1:num_cols;
if tf(i,j)< 50
students = students + 1
end
end
end
fprintf ('Number of students who passed = %0.0f ', students);

  3 Comments

Jon
Jon on 11 Nov 2020
Please add further details about how you go from "main matrix" to tf
Naruto
Naruto on 11 Nov 2020
tf (80x8) is a multiples matrix of column 2 and 3 of the main matrix
dpb
dpb on 11 Nov 2020
Absolutely worthless description from which to try to decipher what it is you're really after.
Don't use cryptic terse tweet-like messages; SHOW us exactly what you have (in part) and what you would expect in return.
See the follow-up below however of one guess of what you might mean.

Sign in to comment.

Answers (1)

dpb
dpb on 11 Nov 2020
Use logical indexing, "the MATLAB way".
LIMIT=50; % don't put magic numbers inside code; use variables so can change
isOK=(tf>=LIMIT) % presuming tf really is only one column as per description and pass is >= LIMIT
nPass=sum(isOK); % add logical ONES to count number passing
nFail=sum(~isOK); % failing is complement (or size(A,1)-nPass)
IDPass=A(isOK,1); % student IDs in column one of Array A at TRUE elements of logical array
fprintf('Passed: %d %.1f\n',IDPass,tf(isOK)) % simple output of ID, score
Above assumes the full array is A; use whatever variable you named it in place of...

  2 Comments

dpb
dpb on 11 Nov 2020
Then need explanation of what it actually is and what you really want -- if it is a grade for multiple exams, say, then probably (guessing here, we don't have enough info to really know) would want to add an any or all to the logical test to find those who passed all/failed any of the eight.
That would look something like:
isOK=all(tf>=LIMIT,2); % student passed 'em all...
The rest would essentially be unchanged except for fixing up the output format string to match the number of elements in tf

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!