MATLAB Interview Questions and Answers: 24 Questions with Worked Code

24 MATLAB interview questions for engineering freshers, from * vs .* and logical indexing to A\b, fzero, ode45, polyfit and Simulink MIL/SIL/HIL testing, each with a direct answer and code whose output we re-computed. Includes what changed in R2025a and R2026b and Skill-Lync placement data for MATLAB-heavy roles.

MATLAB Interview Questions and Answers: 24 Questions with Worked Code

MATLAB interviews for engineering freshers test three things: whether you understand arrays (matrix vs element-wise operations, indexing, preallocation), whether you can write a short script without help, and whether you can use the numerical tools an engineer relies on, such as A\b, fzero, ode45, polyfit and interp1. Model-based design roles add Simulink questions on solvers and MIL/SIL/HIL testing. Below are 24 questions in those four groups, each with a direct answer and, where it helps, code with its output.

Key takeaways

  • The single most-asked distinction is * vs .*: for A = [1 2; 3 4], A*A is [7 10; 15 22] but A.*A is [1 4; 9 16] (Q3).
  • Solve linear systems with x = A\b, not inv(A)*b. MathWorks' own documentation recommends the backslash operator for both speed and accuracy (Q15).
  • GUIDE, MATLAB's old GUI builder, was removed in R2025a; new apps are built in App Designer. Answering "use GUIDE" now dates you (Q23).
  • Every output in this article was re-computed by a script; the ode45 example (Q17) matches the exact solution of a damped spring-mass system to within 0.1 mm.
  • In Skill-Lync's placement records (snapshot of 15 Nov 2026), MBD / controls engineer offers have a median of 3.5 LPA (n = 118), mostly early-career offers from 2021-23.

Which MATLAB questions do interviewers ask freshers?

Expect four kinds: concept questions on arrays, data types and functions; a short live-coding task such as reversing a string or counting values; numerical questions where you solve an equation or simulate a system; and, for automotive and controls roles, Simulink and model-based design questions. The coding part usually decides the outcome.

Interviewers rarely want syntax recited. They want to see that you think in arrays rather than loops, that you check your answer (does the output size make sense? does the result satisfy the equation?), and that you know which built-in function does the job. Each answer below starts with the sentence you would say, then shows the code.

MATLAB fundamentals: questions 1-8

1. What is MATLAB, and what do engineers use it for?

MATLAB is MathWorks' programming language and desktop environment for numerical computing, built around matrices. Engineers use it to analyse test data, design and tune control systems, process signals and images, run numerical methods and, with Simulink, model and generate code for embedded controllers. MathWorks ships two releases a year, named "a" and "b"; R2026b is the current one.

In Indian engineering jobs it shows up most in automotive controls and model-based design, battery and EV modelling, signal processing, and research and test labs. If you are a mechanical engineer wondering whether it is worth your time, is MATLAB of any use to mechanical engineers? walks through the tasks it does on the job.

2. What is the difference between a script and a function?

A script is a list of commands that runs in the workspace you call it from, so it reads and overwrites your existing variables. A function takes inputs, returns outputs and has its own private workspace, so it cannot accidentally change your variables. Use scripts for quick analysis and functions for anything you will reuse or test.

% circleArea.m : a function file; the file name must match the function name
function a = circleArea(r)
    a = pi * r.^2;      % .^ so it also works when r is a vector
end

% at the command line
circleArea([1 2 3])  % 3.1416  12.5664  28.2743

3. What is the difference between *, .*, / and ./?

Operators without a dot follow linear algebra: * is the matrix product (rows times columns) and ^ is a matrix power. Operators with a dot work element by element: .*, ./ and .^ combine matching entries. Using * where you meant .* is the most common MATLAB bug in interviews.

A = [1 2; 3 4];
A * A  % [7 10; 15 22]   matrix product: 7 = 1*1 + 2*3
A .* A  % [1 4; 9 16]     element by element: 9 = 3*3
A ^ 2  % same as A*A
A .^ 2  % same as A.*A
For A = [1 2; 3 4], A*A gives [7 10; 15 22] while A.*A gives [1 4; 9 16]; adding column [1; 2; 3] to row [10 20] expands both to 3 by 2 and gives [11 21; 12 22; 13 23]
Questions 3 and 4 on the same small arrays. Outputs re-computed by the script in this post's run folder.

4. What is implicit expansion?

Implicit expansion lets element-wise operators combine arrays of different but compatible sizes: in each dimension the sizes must be equal or one of them must be 1. MATLAB then behaves as if the size-1 dimension were copied to match. Adding a 3 × 1 column to a 1 × 2 row gives a 3 × 2 result without writing a loop or calling repmat.

[1; 2; 3] + [10 20]
% ans =
%     11    21
%     12    22
%     13    23

A practical use: subtracting each column's mean from a data matrix is just X - mean(X).

5. What is the difference between a cell array, a structure and a table?

A cell array holds anything in each cell and is indexed by position with curly braces. A structure stores values in named fields, like a record. A table holds column-oriented data with named variables of possibly different types, like a spreadsheet, and is the right container for test or sensor data you will filter and plot.

c = {42, 'bolt', [1 2 3]};  % cell array: c{2} is 'bolt'
s.name = 'M8 bolt';  % structure with named fields
s.pitch_mm = 1.25;
T = table([8; 10], [1.25; 1.5], 'VariableNames', {'d_mm', 'pitch_mm'});
T.pitch_mm(2)  % 1.5  (M10 coarse pitch)

6. What is logical indexing?

Logical indexing selects array elements with a true/false mask of the same size instead of a list of positions. A comparison such as v > 0 builds the mask, and v(mask) returns the matching elements. It replaces most "loop and if" code, and you can also assign through it to change only the selected elements.

v = [4 -2 7 0 -5 9];
v(v > 0)  % [4 7 9]
sum(v(v < 0))  % -7
v(v < 0) = 0  % [4 0 7 0 0 9]

7. Why does 0.1 + 0.2 == 0.3 return false?

Because 0.1, 0.2 and 0.3 cannot be stored exactly in binary double precision, so the sum differs from 0.3 by about 5.55 × 10⁻¹⁷. Comparing floating-point results with == is therefore unreliable. Compare with a tolerance instead, scaled to the size of the numbers you are working with.

0.1 + 0.2 == 0.3  % logical 0 (false)
abs((0.1 + 0.2) - 0.3)  % 5.5511e-17
abs((0.1 + 0.2) - 0.3) < 1e-12  % logical 1 (true)

MATLAB's eps, the gap between 1 and the next larger double, is about 2.2204 × 10⁻¹⁶; a relative tolerance a few hundred times that is a common choice.

8. What is the difference between a character array and a string?

A character array, written with single quotes, is a row of individual characters, so 'hello' is 1 × 5. A string, written with double quotes, is one object, so "hello" is 1 × 1. String arrays were introduced in R2016b and make lists of text easier: each element is a whole piece of text.

size('hello')  % 1 5
size("hello")  % 1 1
['ab' 'cde']  % 'abcde'          characters joined into one row
["ab" "cde"]  % 1x2 string array: "ab"  "cde"

MATLAB coding questions: 9-14

9. How do you check whether a word is a palindrome?

Convert the text to one case, reverse it with fliplr, and compare with isequal. That is three lines and no loop. Mention that for real text you would first strip spaces and punctuation, for example with isletter, so that "Never odd or even" also passes.

s = 'Racecar';
t = lower(s);
isPal = isequal(t, fliplr(t))  % logical 1 (true)

10. Print the first 10 Fibonacci numbers, and why preallocate?

Create the output array at its final size with zeros before the loop, then fill it. MathWorks' documentation notes that growing an array inside a loop can hurt both speed and memory use, because MATLAB keeps finding new memory for the bigger array; preallocating avoids that.

n = 10;
f = zeros(1, n);  % preallocate
f(2) = 1;
for k = 3:n
    f(k) = f(k-1) + f(k-2);
end
disp(f)  % 0  1  1  2  3  5  8  13  21  34

11. Find the largest value, its position, and the second-largest value

Call max with two outputs to get the value and the index of its first occurrence. Use find to get every position of the maximum, and unique, which returns sorted distinct values, to get the second-largest distinct value. Always say what happens with ties; interviewers ask.

x = [3 9 2 9 5];
[m, i] = max(x)  % m = 9, i = 2  (first occurrence)
find(x == m)  % [2 4]         (all occurrences)
u = unique(x);  % [2 3 5 9]
second = u(end-1)  % 5

12. Count how many times each value appears

Use unique with its third output, which maps every element to its position in the list of unique values, then count with accumarray. For quick work, groupcounts or histcounts also do it. The point interviewers look for is avoiding a nested loop over every pair of elements.

x = [2 3 2 5 3 2];
[u, ~, j] = unique(x);  % u = [2 3 5]
counts = accumarray(j(:), 1)'  % [3 2 1]

13. How do you vectorise a loop?

Replace the loop with an operation on the whole array. The sum of squares from 1 to 100 needs no loop: build the vector 1:100, square it element-wise and sum. Vectorised code is shorter, easier to check and usually faster, because the work happens in MATLAB's compiled built-in functions.

s = sum((1:100).^2)  % 338350   (check: 100*101*201/6 = 338350)

14. How do you handle missing values (NaN) in data?

Any arithmetic with NaN returns NaN, so a plain mean of data with a missing reading is NaN. Either pass the 'omitnan' flag to statistics functions, or remove the missing entries with a logical mask from isnan (or rmmissing). Say which one you chose and why: dropping rows can bias a result.

d = [12 NaN 15 18 NaN 20];
mean(d)  % NaN
mean(d, 'omitnan')  % 16.25
d(~isnan(d))  % [12 15 18 20]

Numerical methods and engineering questions: 15-19

15. How do you solve a system of linear equations, and why not use inv?

Use the backslash operator, x = A\b. MATLAB picks a suitable factorisation for the matrix and solves directly. MathWorks' documentation for inv recommends backslash instead of inv(A)*b, as faster and more accurate. Then check the answer by computing the residual A*x - b.

A = [4 -2 1; -2 4 -2; 1 -2 4];
b = [11; -16; 17];
x = A \ b  % [1; -2; 3]
norm(A*x - b)  % ~0, check the residual

16. How do you find the root of a nonlinear equation?

Use fzero for one equation in one unknown: give it a function handle and a starting guess or a bracket where the function changes sign. For polynomials, roots returns every root, including complex ones. The classic test equation has one real root, 2.0946.

f = @(x) x.^3 - 2*x - 5;
r = fzero(f, 2)  % 2.0946
roots([1 0 -2 -5])  % 2.0946 and a complex pair -1.0473 ± 1.1359i

17. How do you simulate a spring-mass-damper with ode45?

Rewrite the second-order equation as two first-order equations, with position and velocity as the state, pass them to ode45 with a time span and initial conditions, and plot the first column of the result. ode45 uses an explicit Runge-Kutta (4,5) Dormand-Prince pair.

m = 1; c = 0.4; k = 4;  % kg, N*s/m, N/m
rhs = @(t, y) [y(2); -(c*y(2) + k*y(1))/m];  % y = [x; v]
[t, y] = ode45(rhs, [0 10], [0.1; 0]);  % released from 0.1 m at rest
plot(t, y(:,1)), xlabel('t (s)'), ylabel('x (m)')

Check the result by hand, as you would in an interview. The natural frequency and damping ratio are

so the system is under-damped. The damped period is , and each positive peak is times the one before. The plot below shows the same Runge-Kutta method sitting on the exact solution.

Displacement of a spring-mass-damper with m = 1 kg, c = 0.4 N s/m, k = 4 N/m released from 0.1 m: a decaying oscillation with damped period 3.16 s, each peak 0.532 times the previous, Runge-Kutta 4(5) steps lying on the exact curve within 0.1 mm
Question 17: the Dormand-Prince Runge-Kutta steps (amber) lie on the exact response (blue); the dashed lines are the decay envelope.

Add a tyre spring and a road input and the same model becomes the quarter-car suspension used in vehicle dynamics; Skill-Lync's Vehicle Dynamics using MATLAB course builds it out.

18. How do you fit a straight line to test data?

Use polyfit(x, y, 1) for a least-squares straight line; it returns the slope and intercept, highest power first. Evaluate the fit with polyval. Always plot the data with the fit and look at the residuals before trusting it, and be careful extrapolating beyond the measured range.

x = [1 2 3 4 5];
y = [2.2 4.1 6.1 7.9 10.2];
p = polyfit(x, y, 1)  % [1.98 0.16]  slope, intercept
polyval(p, 6)  % 12.04

19. How do you use a lookup table, for example an engine torque curve?

Use interp1 to interpolate between measured breakpoints, which is what a 1-D lookup table block does in Simulink. Linear interpolation is the default. Decide what should happen outside the table: by default interp1 returns NaN there, and in a controller you normally clamp instead.

rpm    = [0 1000 2000 3000];
torque = [0 120 180 200];  % N*m
interp1(rpm, torque, 2500)  % 190

For the Simulink side of the same idea, see how to use a lookup table in Simulink.

Simulink and model-based design questions: 20-24

20. What is Simulink, and how is it different from MATLAB?

Simulink is MathWorks' block-diagram environment for modelling and simulating dynamic systems over time, such as a motor and its controller. MATLAB is text-based programming. They work together: MATLAB scripts set parameters and analyse results, Simulink holds the time-domain model, and Embedded Coder turns that model into C code for an ECU.

A longer comparison, with when to use which, is in the difference between Simulink and MATLAB.

21. Fixed-step or variable-step solver: which do you choose?

Variable-step solvers change the step size to meet accuracy targets and are efficient for desktop simulation. Fixed-step solvers use one constant step, which is what an embedded controller runs in real time. MathWorks' documentation states that generating code for deployment requires a fixed-step solver, so model-based design teams use fixed-step for controller models.

A good interview answer adds a practical step: simulate with a variable-step solver first to see what step size the dynamics need, then choose the fixed step for the deployed model from that.

22. What are MIL, SIL, PIL and HIL testing?

They test the same controller at increasing levels of realism. Model-in-the-loop (MIL) tests the Simulink model itself. Software-in-the-loop (SIL) compiles the generated code and runs it on your PC to check it matches the model. Processor-in-the-loop (PIL) runs it on the target processor. Hardware-in-the-loop (HIL) connects the real controller to a real-time simulated plant.

StageWhat runsWhat it catches
MILSimulink model of the controller and plantWrong control logic, early
SILGenerated code on the host PCModel vs code mismatches
PILGenerated code on the target processorCompiler, timing and processor effects
HILReal ECU with a real-time plant modelHardware, I/O and real-time faults

Verification and validation roles in automotive spend most of their time here. Skill-Lync's software verification and validation for model-based development course covers the workflow, and the AUTOSAR interview questions post covers the software architecture that sits around the generated code.

23. How do you build a GUI in MATLAB today?

Use App Designer, MATLAB's current environment for building apps: drag components onto a canvas and write callbacks in the same file. GUIDE, the older drag-and-drop builder that many tutorials still describe, was removed in R2025a. Existing GUIDE apps still run, and MathWorks provides a migration tool to move them to App Designer.

For simple programmatic interfaces you can also create components in code with uifigure, uibutton and similar functions.

24. What is P-code, and does it protect your code?

The pcode function converts a .m file into an obfuscated, execute-only .p file that runs like the original but cannot be read as source. MathWorks describes it as obfuscation, and its documentation advises combining approaches to protect sensitive code. Since R2022a, a -R2022a option uses a more complex obfuscation algorithm.

The honest interview answer: P-code stops casual reading of your source, but it is not encryption, so do not rely on it alone for secrets such as keys or licensing logic.

What changed in MATLAB recently that interviewers may ask about?

Three changes are worth knowing: GUIDE was removed in R2025a in favour of App Designer; R2026b, the current release, no longer installs a Java runtime with MATLAB (an OpenJDK add-on is available instead); and MathWorks continues its pattern of two releases a year. Mentioning the current release name shows you use the tool, not old tutorials.

Which jobs ask MATLAB questions, and what do they pay in India?

MATLAB and Simulink questions dominate interviews for model-based design and controls engineers, battery and EV modelling engineers, and many embedded and verification roles in automotive. In Skill-Lync's placement records, MBD / controls engineer offers have a median of 3.5 LPA (n = 118) and battery / EV engineer offers 3.5 LPA (n = 138).

Skill-Lync placement records, snapshot 15 Nov 2026 (placements up to 7 Nov 2026). Salaries in LPA as reported; P25-P75 is the middle half.
RolenMedianP25-P75P90
MBD / controls engineer1183.53.5-4.285.0
Battery / EV engineer1383.53.5-4.55.93
Embedded engineer1554.03.5-5.06.0
All roles, freshers2,4303.53.0-4.25.0

The employers that hired Skill-Lync's EV-track learners most often were Tata Elxsi (60 placements), Expleo (47), Ola Electric (28) and Stanadyne (21); in the smaller MBD track, Expleo led with 12. These figures are based on 3,711 Skill-Lync placement records up to 7 Nov 2026 (data snapshot of 15 Nov 2026), from learners who reported a placement, so they are not a survey of the whole market. Most records are starting offers from 2021-23. It is a starting point, and we will update it as new placements come in. More detail is on the EV engineer salary page and the embedded engineer salary page.

How should you prepare for a MATLAB interview?

Type out every snippet above and change it until you can predict the output before running it. Then build one small project end to end, such as the spring-mass-damper or a battery model, with a plot and a hand check. MATLAB Online (basic) is free in the browser for 20 hours a month.

  1. Get access. Is MATLAB free for students? explains the free and paid options in India.
  2. Cover the basics. The free MATLAB Basics and MATLAB and Simulink Basics lessons on Skill-Lync Grow cover questions 1-14.
  3. Go deeper for your target role. MATLAB and Simulink Basics suits electrical and EV roles, and MATLAB for Mechanical Engineers suits design, CAE and vehicle roles. Every option is listed on the MATLAB courses page.
  4. Show a project. Pick one from the MATLAB project ideas list and be ready to explain every line.

Not sure which route suits your branch and target role? Talk to a Skill-Lync counsellor; it is free.

Frequently asked questions

Is MATLAB still in demand for engineering jobs in India?

Yes, in specific areas. MATLAB and Simulink are the standard tools for model-based design of automotive controllers, battery and motor modelling, and much signal-processing and test work. Pure software roles use it far less. In Skill-Lync's placement records, MBD / controls and battery / EV roles together account for 256 placements with salary data.

Can I answer MATLAB coding questions in Python instead?

Only if the interviewer agrees. Python with NumPy and SciPy can do every numerical task in this article, and many teams use both. But a role that lists MATLAB or Simulink expects MATLAB syntax, especially array indexing from 1, element-wise operators and function handles, so practise the answers in MATLAB itself.

Which MATLAB topics do freshers get asked most?

Matrix versus element-wise operations, indexing and logical indexing, scripts versus functions, preallocation and vectorisation, and solving linear systems with backslash come up most for freshers. Controls and EV roles add ode45, lookup tables, Simulink solvers and MIL/SIL/HIL testing. Questions 3, 6, 10, 15 and 21 in this article cover the core of it.

Do I need Simulink for an MBD interview?

Yes. Model-based design work is done in Simulink and Stateflow, with Embedded Coder generating the production code, so interviews ask about solvers, sample times, subsystems, lookup tables, data dictionaries and MIL/SIL/HIL testing. Expect to sketch a small controller model and explain how you would test it at each stage, not only to write MATLAB scripts.

Is GUIDE still used for MATLAB GUIs?

No. MathWorks removed GUIDE in R2025a. Existing GUIDE apps still run and can be migrated, but new apps should be built in App Designer or programmatically with uifigure-based components. Older interview guides that tell you to type "guide" at the prompt are out of date, so answer with App Designer instead.

Sources and further reading

Want this on your résumé?

Talk to a counsellor.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.