Land the job you want — prepare
with Real interviews Q&A
Curated interview questions, company-wise guides and coding rounds. Practice mock interviews, improve with feedback, and track your progress.
MATLAB (Matrix Laboratory) is a high-level programming language and interactive environment developed by MathWorks. It is widely used for numerical computing, matrix manipulations, data analysis, visualization, and algorithm development.
Variables in MATLAB are used to store data values. They are dynamically typed, which means you do not need to explicitly declare their data type or allocate memory before using them.
A matrix is the fundamental data structure in MATLAB. MATLAB is designed to treat all variables as multi-dimensional arrays or matrices, which allows for highly optimized mathematical operations.
A row vector can be created by separating element values with spaces or commas inside square brackets:
a = [1 2 3 4];A column vector is created by separating element values with semicolons inside square brackets:
a = [1; 2; 3; 4];Placing a semicolon (;) at the end of a line suppresses output display in the Command Window, allowing statements to execute quietly in the background.
Comments are initiated with the percent symbol (%). Anything following it on the same line is ignored by the compiler:
% This is a single-line comment in MATLABA script is a standard text file with a .m extension containing a sequential list of MATLAB commands. Running the script executes these statements as if they were typed directly into the command line.
A function is a separate block of code that accepts parameters, runs isolated computations in its own workspace, and returns specific output variables.
Scripts operate within the global base workspace, sharing variable declarations directly.Functions maintain an isolated, temporary local workspace that clears automatically when execution ends.
Functions are declared using the function keyword, mapping outputs, the function name, and inputs:
function y = squareNum(x)
y = x^2;
endIndexing refers to accessing individual elements or sub-sections of arrays and matrices. MATLAB uses 1-based indexing, meaning the first element in any array starts at index 1.
The colon operator (:) generates sequences of values and is useful for creating loops or slicing index matrices:
% Generate sequence from 1 to 5
seq = 1:5;A cell array is a dynamic database structure containing indexed buckets called cells, where each individual cell can store different data types, structures, and dimensions.
Structures group data logically using named parameter fields, letting you map associated attributes to a single object:
person.name = 'John';
person.age = 30;Vectorization is the process of replacing explicit loops (like for and while) with matrix algebra equations. Since MATLAB operations are highly optimized for matrices, vectorized code runs significantly faster.
A Value class creates a completely new copy of an object when it is assigned or passed to a function. A Handle class passes objects by reference, meaning modifications in one location affect all variables referencing that instance.
A sparse matrix is an optimized matrix structure that only stores elements with non-zero values. This saves memory and calculation overhead when working with large matrices containing mostly zeroes.
Simulink is a block-diagram, visual programming environment integrated directly with MATLAB. It is used for modeling, simulating, and analyzing multi-domain dynamic and embedded control systems.
A MEX file (MATLAB Executable) is a compiled C, C++, or Fortran subroutine that runs directly inside MATLAB, allowing you to optimize computationally heavy tasks.
n = 5;
fact = 1;
for i = 1:n
fact = fact * i;
enda = [1 2 3 4];
rev = fliplr(a);n = 7;
is_prime_result = isprime(n);a = [1 2 3 4];
s = sum(a);a = [1 12 3 4];
maxVal = max(a);A = [1 2; 3 4];
B = A';x = 0:0.1:2*pi;
y = sin(x);
plot(x, y);I = eye(3);str = 'Hello';
len = length(str);a = 5;
b = 10;
temp = a;
a = b;
b = temp;