Bird
0
0

You have a structure array students with fields name and score. How do you create a new structure passed containing only students with score >= 50?

hard📝 Application Q15 of 15
MATLAB - Cell Arrays and Structures
You have a structure array students with fields name and score. How do you create a new structure passed containing only students with score >= 50?
Apassed = students([students.score] >= 50);
Bpassed = struct('name', students.name, 'score', students.score >= 50);
Cpassed = students.score >= 50;
Dpassed = students{students.score >= 50};
Step-by-Step Solution
Solution:
  1. Step 1: Understand structure arrays and logical indexing

    In MATLAB, [students.score] creates an array of scores. Logical indexing selects elements where condition is true.
  2. Step 2: Apply logical indexing to filter students

    The expression students([students.score] >= 50) selects only students with scores 50 or above.
  3. Final Answer:

    passed = students([students.score] >= 50); -> Option A
  4. Quick Check:

    Use logical indexing on structure arrays [OK]
Quick Trick: Use logical indexing with [struct.field] to filter arrays [OK]
Common Mistakes:
  • Trying to assign logical array directly
  • Using curly braces instead of parentheses
  • Creating new struct without filtering

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes