Bird
0
0

You want to save multiple numpy arrays a and b into a single .mat file and later load them back. Which code correctly saves and loads these arrays so you can access them by their variable names?

hard📝 Application Q15 of 15
SciPy - Integration with Scientific Ecosystem
You want to save multiple numpy arrays a and b into a single .mat file and later load them back. Which code correctly saves and loads these arrays so you can access them by their variable names?
Aio.savemat('multi.mat', [a, b]) data = io.loadmat('multi.mat') print(data['a'], data['b'])
Bio.savemat('multi.mat', a, b) data = io.loadmat('multi.mat') print(data['a'], data['b'])
Cio.savemat('multi.mat', {'a': a, 'b': b}) data = io.loadmat('multi.mat') print(data['multi']['a'], data['multi']['b'])
Dio.savemat('multi.mat', {'a': a, 'b': b}) data = io.loadmat('multi.mat') print(data['a'], data['b'])
Step-by-Step Solution
Solution:
  1. Step 1: Save multiple arrays with a dictionary

    Use a dictionary with keys as variable names and values as arrays in savemat. io.savemat('multi.mat', {'a': a, 'b': b}) data = io.loadmat('multi.mat') print(data['a'], data['b']) does this correctly.
  2. Step 2: Load and access arrays by keys

    After loading, access arrays by their keys 'a' and 'b' in the loaded dictionary. io.savemat('multi.mat', {'a': a, 'b': b}) data = io.loadmat('multi.mat') print(data['a'], data['b']) prints them correctly.
  3. Final Answer:

    io.savemat('multi.mat', {'a': a, 'b': b}) data = io.loadmat('multi.mat') print(data['a'], data['b']) -> Option D
  4. Quick Check:

    Save/load dict with keys = variable access [OK]
Quick Trick: Save dict with names, load dict and access keys [OK]
Common Mistakes:
  • Passing list instead of dict to savemat
  • Trying to access nested keys incorrectly
  • Passing multiple args to savemat instead of one dict

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes