Bird
0
0

Given a structure array movies with fields Title and Duration (in minutes), which MATLAB code snippet correctly creates a new structure array shortMovies containing only movies shorter than 90 minutes?

hard📝 Application Q8 of 15
MATLAB - Cell Arrays and Structures
Given a structure array movies with fields Title and Duration (in minutes), which MATLAB code snippet correctly creates a new structure array shortMovies containing only movies shorter than 90 minutes?
AshortMovies = movies(movies.Duration < 90);
BshortMovies = movies([movies.Duration] < 90);
CshortMovies = struct('Title', movies.Title([movies.Duration] < 90), 'Duration', movies.Duration([movies.Duration] < 90));
DshortMovies = movies(find(movies.Duration) < 90);
Step-by-Step Solution
Solution:
  1. Step 1: Extract durations

    Use [movies.Duration] to get an array of durations.
  2. Step 2: Logical indexing

    Create a logical index for durations less than 90.
  3. Step 3: Index structure array

    Use the logical index to select elements from movies.
  4. Final Answer:

    shortMovies = movies([movies.Duration] < 90); correctly filters the structure array.
  5. Quick Check:

    Verify shortMovies contains only movies with duration < 90. [OK]
Quick Trick: Use logical indexing with field arrays [OK]
Common Mistakes:
  • Using parentheses instead of square brackets for field extraction
  • Applying logical conditions directly on structure arrays
  • Incorrect use of find function with logical indexing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes