필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

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

조회 수: 1 (최근 30일)
Naruto
Naruto 2020년 11월 11일
마감: MATLAB Answer Bot 2021년 8월 20일
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
Naruto
Naruto 2020년 11월 11일
tf (80x8) is a multiples matrix of column 2 and 3 of the main matrix
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
Naruto
Naruto 2020년 11월 11일
What if tf was 80x8?
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