Bird
0
0

Given a structure array books with fields Title and Pages, how do you create a new structure array shortBooks containing only books with less than 100 pages?

hard📝 Application Q15 of 15
MATLAB - Cell Arrays and Structures
Given a structure array books with fields Title and Pages, how do you create a new structure array shortBooks containing only books with less than 100 pages?
AshortBooks = books(books.Pages < 100);
BshortBooks = books([books.Pages] < 100);
CshortBooks = books{books.Pages < 100};
DshortBooks = books(books.Pages > 100);
Step-by-Step Solution
Solution:
  1. Step 1: Extract numeric array of Pages

    Use [books.Pages] to get all Pages values as a numeric array.
  2. Step 2: Use logical indexing to filter

    Compare with < 100 to get logical indices, then index books with these.
  3. Step 3: Check syntax correctness

    shortBooks = books([books.Pages] < 100); correctly uses parentheses and logical indexing; others are incorrect.
  4. Final Answer:

    shortBooks = books([books.Pages] < 100); -> Option B
  5. Quick Check:

    Logical indexing with [books.Pages] works [OK]
Quick Trick: Use [struct.field] for numeric indexing [OK]
Common Mistakes:
  • Using curly braces {} instead of parentheses ()
  • Forgetting to use brackets [] around field extraction
  • Using wrong comparison operator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes