Bird
0
0

You want to plot multiple data sets but only show plots where y-values are positive. How can you combine hold on with conditional plotting?

hard📝 Application Q9 of 15
MATLAB - 2D Plotting
You want to plot multiple data sets but only show plots where y-values are positive. How can you combine hold on with conditional plotting?
Aplot(x, y1); hold on; plot(x(y2>0), y2(y2>0)); hold off;
Bhold on; plot(x, y1); plot(x, y2); hold off;
Cplot(x, y1); plot(x, y2(y2>0)); hold on;
Dhold off; plot(x, y1(y1>0)); hold on; plot(x, y2);
Step-by-Step Solution
Solution:
  1. Step 1: Plot first data normally

    plot(x, y1) plots all y1 values.
  2. Step 2: Use logical indexing to plot only positive y2 values

    plot(x(y2>0), y2(y2>0)) plots only points where y2 is positive.
  3. Step 3: Use hold on to combine plots and hold off after

    This ensures both plots appear on the same figure.
  4. Final Answer:

    plot(x, y1); hold on; plot(x(y2>0), y2(y2>0)); hold off; -> Option A
  5. Quick Check:

    Use logical indexing with hold on to filter plots = D [OK]
Quick Trick: Use logical indexing with hold on to filter plots [OK]
Common Mistakes:
  • Plotting all points without filtering
  • Misplacing hold on and hold off
  • Incorrect logical indexing syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes