Multiple outputs in MATLAB - Time & Space Complexity
We want to see how the time needed changes when a MATLAB function gives back more than one answer.
How does asking for multiple results affect the work the program does?
Analyze the time complexity of the following code snippet.
function [sumVal, prodVal] = sumAndProduct(arr)
n = length(arr);
sumVal = 0;
prodVal = 1;
for i = 1:n
sumVal = sumVal + arr(i);
prodVal = prodVal * arr(i);
end
end
This function calculates the sum and product of all elements in an array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single loop that goes through each element of the array once.
- How many times: Exactly as many times as there are elements in the array (n times).
As the array gets bigger, the loop runs more times, doing a fixed amount of work each time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 additions and 10 multiplications |
| 100 | About 100 additions and 100 multiplications |
| 1000 | About 1000 additions and 1000 multiplications |
Pattern observation: The work grows directly with the number of elements. Double the elements, double the work.
Time Complexity: O(n)
This means the time to get both outputs grows in a straight line with the size of the input array.
[X] Wrong: "Getting two outputs means the program does twice as much work, so the time complexity doubles to O(2n)."
[OK] Correct: The loop still runs once per element doing both tasks together, so the time grows linearly, not doubled in complexity terms.
Understanding how multiple outputs affect time helps you explain your code clearly and shows you know how work scales in real programs.
"What if the function used two separate loops instead of one to calculate sum and product? How would the time complexity change?"
Practice
Solution
Step 1: Understand MATLAB function output syntax
MATLAB requires multiple outputs to be enclosed in square brackets in the function definition. Only Use square brackets around output variables in the function definition. correctly uses square brackets; others are invalid syntax or incorrect approach.Final Answer:
Use square brackets around output variables in the function definition. -> Option BQuick Check:
Multiple outputs = square brackets [OK]
- Using parentheses instead of square brackets
- Listing outputs without any brackets
- Trying to define outputs in separate functions
myFunc that returns two outputs?Solution
Step 1: Recall MATLAB function call syntax for multiple outputs
Multiple outputs must be captured using square brackets on the left side of the assignment. [a, b] = myFunc(x); uses correct square brackets; B and C use invalid syntax; A calls function twice, not multiple outputs.Final Answer:
[a, b] = myFunc(x); -> Option AQuick Check:
Multiple outputs call = square brackets [OK]
- Using parentheses or commas without brackets
- Calling function multiple times instead of one call
- Missing brackets around output variables
function [sumVal, diffVal] = calc(a, b)
sumVal = a + b;
diffVal = a - b;
end
[x, y] = calc(5, 3);What are the values of
x and y?Solution
Step 1: Trace function execution and match outputs
The function returns sumVal = a + b = 5 + 3 = 8 and diffVal = a - b = 5 - 3 = 2. x receives sumVal (8), y receives diffVal (2).Final Answer:
x = 8, y = 2 -> Option CQuick Check:
sum = 8, diff = 2 [OK]
- Swapping order of outputs
- Confusing inputs with outputs
- Adding instead of subtracting for difference
function [a, b] = wrongFunc(x)
a = x * 2;
b = x + 3
endSolution
Step 1: Check syntax line by line
Line assigning b lacks a semicolon, causing output to print unexpectedly. Multiple outputs with square brackets are allowed; function name matching file is good practice but not error here.Final Answer:
Missing semicolon after assigning b -> Option DQuick Check:
Missing semicolon = syntax error [OK]
- Ignoring missing semicolons
- Thinking multiple outputs are not allowed
- Confusing function name and file name mismatch as error
stats that takes a vector and returns three outputs: the mean, median, and standard deviation. Which function header and call are correct?Solution
Step 1: Verify function header and call syntax
Multiple outputs must use square brackets in the function header: function [m, med, s] = stats(v) and in the call: [meanVal, medianVal, stdVal] = stats(data);. Others use invalid parentheses, commas without brackets, or incorrect formats.Final Answer:
Function and call with square brackets for multiple outputs -> Option AQuick Check:
Multiple outputs = square brackets in header and call [OK]
- Using parentheses instead of square brackets
- Not capturing all outputs in call
- Incorrect function header syntax
Ready to prove it?
Pass the test and this pattern moves unknown
