Info

此问题已关闭。 请重新打开它进行编辑或回答。

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

3 次查看(过去 30 天)
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 个评论
dpb
dpb 2020-11-11
编辑:dpb 2020-11-11
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.

回答(1 个)

dpb
dpb 2020-11-11
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 个评论
dpb
dpb 2020-11-11
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

Community Treasure Hunt

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

Start Hunting!

Translated by