Input and output arguments in MATLAB - Time & Space Complexity
We want to understand how the time a MATLAB function takes changes when we change its inputs or outputs.
How does the size or number of input and output arguments affect the work done?
Analyze the time complexity of the following MATLAB function using input and output arguments.
function y = squareElements(x)
n = length(x);
y = zeros(1, n);
for i = 1:n
y(i) = x(i)^2;
end
end
This function takes a vector x as input and returns a vector y where each element is the square of the corresponding element in x.
Look for loops or repeated steps that depend on input size.
- Primary operation: The
forloop that squares each element. - How many times: It runs once for each element in the input vector
x, sontimes.
As the input vector gets longer, the function does more work.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 squaring steps |
| 100 | 100 squaring steps |
| 1000 | 1000 squaring steps |
Pattern observation: The work grows directly with the number of input elements. Double the input size, double the work.
Time Complexity: O(n)
This means the time to run the function grows in a straight line with the input size.
[X] Wrong: "The function runs in constant time because it just squares numbers."
[OK] Correct: Even though squaring one number is quick, the function does it for every element, so more elements mean more work.
Understanding how input size affects function time helps you explain your code clearly and shows you can think about efficiency in real projects.
"What if the function returned two output vectors instead of one? How would the time complexity change?"
Practice
Solution
Step 1: Understand input arguments
Input arguments are values or data you give to a function so it can use them inside.Step 2: Identify the purpose
The main purpose is to send data into the function for it to work on.Final Answer:
To send data into the function for processing -> Option AQuick Check:
Input arguments = send data in [OK]
- Confusing input arguments with output arguments
- Thinking input arguments display results
- Assuming input arguments create new functions
Solution
Step 1: Recall MATLAB function syntax
The correct syntax starts with 'function', then output, '=', function name, and input in parentheses.Step 2: Match the syntax
function output = myFunc(input) matches this pattern exactly: function output = myFunc(input)Final Answer:
function output = myFunc(input) -> Option AQuick Check:
Syntax = function output = name(input) [OK]
- Placing output after function name without '='
- Missing parentheses around input
- Using '=' incorrectly in function header
function y = squarePlusOne(x)
y = x^2 + 1;
end
result = squarePlusOne(3);Solution
Step 1: Calculate the square of input
Input x is 3, so x^2 = 3^2 = 9.Step 2: Add 1 to the squared value
9 + 1 = 10.Final Answer:
10 -> Option BQuick Check:
3 squared plus 1 = 10 [OK]
- Forgetting to add 1 after squaring
- Using multiplication instead of power
- Confusing output with input
function output = addNumbers(a, b)
output = a + b
endSolution
Step 1: Check MATLAB syntax rules
Each statement should end with a semicolon to suppress output in functions.Step 2: Identify missing semicolon
The line 'output = a + b' lacks a semicolon at the end.Final Answer:
Missing semicolon after output assignment -> Option DQuick Check:
Statements need semicolons [OK]
- Ignoring semicolon causes unwanted output
- Thinking function names must be capitalized
- Using wrong brackets for inputs
Solution
Step 1: Recall syntax for multiple outputs
Multiple outputs are enclosed in square brackets before the '=' sign.Step 2: Match correct syntax
function [sum, product] = calculate(a, b) uses square brackets for outputs and correct order: function [sum, product] = calculate(a, b)Final Answer:
function [sum, product] = calculate(a, b) -> Option CQuick Check:
Multiple outputs use square brackets [OK]
- Omitting square brackets for multiple outputs
- Placing outputs after function name without brackets
- Using '=' incorrectly in function header
Ready to prove it?
Pass the test and this pattern moves unknown
