MATLAB Assignment-1
NGF COLLEGE OF ENGINEERING AND TECHNOLOGY, PALWAL
Subject code: PCC-CS-302
MATLAB
B.TECH 3rd SEMESTER
Assignment-1
Date of Submission: 21 SEPTEMBER 2025
Q1. What is MATLAB? Write its data types, variables and also its keywords and
constant.
MATLAB (Matrix Laboratory) is a high-level language and interactive environment for
numerical computation, visualization, and programming. It is optimized for matrix and
vector operations, plotting, algorithm development, and data analysis.
Variables & naming rules:
- Variable names start with a letter, followed by letters, digits, or underscores.
- MATLAB is case-sensitive.
- Avoid using function names as variables.
Common Data Types:
- double (default numeric type)
- single (single-precision float)
- Integers: int8, int16, int32, int64, uint8, uint16, uint32, uint64
- logical (true/false)
- char, string (text)
- cell (heterogeneous containers)
- struct (structures)
- table, datetime, duration, categorical
Examples of variables in MATLAB code:
a = 3.14; % double
b = single(2.5); % single
c = int32(100); % integer
flag = true; % logical
name = "Rahul"; % string
chars = 'A'; % char
C = {1, "text", [1 2 3]}; % cell
S.name = "Rahul"; S.age = 20; % struct
Keywords: for, while, if, elseif, else, end, function, return, break, continue, switch, case,
otherwise, try, catch, global, persistent, classdef, properties, methods, parfor, spmd,
arguments
Common Constants: pi, eps, Inf, NaN, realmax, realmin, i, j, true, false
Q2. What is loop in MATLAB? Write each type of loop and its code.
A loop is used to repeat a block of code multiple times. MATLAB supports mainly 'for' and
'while' loops, along with nested loops and parallel loops (parfor).
For loop Example:
sumVal = 0;
for k = 1:5
sumVal = sumVal + k;
end
disp(['Sum 1..5 = ' num2str(sumVal)]);
While loop Example:
n = 5; result = 1; k = 1;
while k <= n
result = result * k;
k = k + 1;
end
disp(['5! = ' num2str(result)]);
Nested loop Example:
A = zeros(3,3);
count = 1;
for i = 1:3
for j = 1:3
A(i,j) = count;
count = count + 1;
end
end
disp(A);
Q3. Write arithmetic, relational, logical, bitwise operators with one code.
The following MATLAB code demonstrates all four categories of operators:
% Arithmetic
a = 10; b = 3;
addAB = a + b; subAB = a - b; mulAB = a * b; divAB = a / b;
modAB = mod(a, b); powAB = a ^ b;
% Relational
(a == b); (a ~= b); (a > b); (a < b); (a >= b); (a <= b);
% Logical
x = (a > 5); y = (b < 5);
x & y; x | (b>10); ~x; x && y; x || (b>10);
% Bitwise
u = uint8(6); v = uint8(3);
bitand(u,v); bitor(u,v); bitxor(u,v);
bitshift(u,1); bitshift(v,-1);