Theory Assignments Module 1&2
Theory Assignments Module 1&2
Group A.
1.How do you start MATLAB, and what are the basic components of its user interface?
Ans – MATLAB can be started from from the MATWORKS website. After opening the website we
have to click Get MATLAB option, the after logging in we can get the interface of it.
Basic components of its user interface are Command Window, Workspace, Current Folder,
Command History, Editor, Figure Window, Toolstrip, Status Bar, Help Browser.
Ans- We can type quit in that window, like this, >> quit and hit Enter.
Ans- MATLAB has several predefined variables that are automatically available when we start a
session. For examples – ‘ans’ ‘pi’ ‘realmax’.
Ans- A "vector" is a one-dimensional array that can hold a sequence of numbers. It can be either
a row vector (1xN) or a column vector (Nx1), where N is the number of elements. Vectors are
fundamental data structures in MATLAB used for storing and manipulating data.
Ans- A matrix is a two-dimensional array with rows and columns, specifically used for linear
algebra operations. An array, on the other hand, is a more general data structure that can have
multiple dimensions (e.g., 1D, 2D, 3D, etc.) and is used for storing data of any size or shape.
Ans- The colon operator (:) in MATLAB is used to create vectors, specify ranges, and select
elements in arrays or matrices.
Ans- To access elements of a vector in MATLAB, we use parentheses () with the index of the
element.
8. What is the significance of built-in functions in MATLAB? Give examples.
Ans- Built-in functions in MATLAB are pre-defined, optimized functions that simplify complex
computations and enhance productivity. They cover a wide range of tasks, including
mathematical operations, data analysis, and visualization. Eg – ‘sum()’ ‘mean()’ ‘plot()’.
Ans- MATLAB handles strings as arrays of characters, which can be created using double quotes
(" "). MATLAB supports string manipulation through various functions like strcat() for
concatenation, strfind() for searching, and length() for finding the length of a string.
Ans- The size function in MATLAB returns the dimensions of an array or matrix. It provides the
number of rows and columns for 2D arrays or the size of each dimension for higher-dimensional
arrays.
Group B.
Ans- Linspace: This function generates a linearly spaced vector. It creates a vector with a
specified number of points between two given values. It is particularly useful when you need to
create a range of values with equal spacing.
Logspace: This function generates a logarithmically spaced vector. It creates a vector with points
that are spaced logarithmically between decades, which is useful for creating frequency vectors
or data on a logarithmic scale.
2. Create a 5x5 matrix with elements that are random integers between 1 and 10, and then find
the trace of the matrix.
Ans-
trace_A = trace(A);
2.Describe the concept of cell arrays in MATLAB and provide an example to create a cell array.
Ans- Cell arrays in MATLAB are data structures that allow you to store arrays of different types
and sizes. Unlike regular arrays, which can only store elements of the same data type and size,
cell arrays can contain heterogeneous data such as strings, numbers, matrices, or even other cell
arrays.
Example- C = {1, 'text', [1, 2, 3];
4. Take a 3x3 matrix, extract the diagonal elements and find their sum.
Ans- A = [1 2 3; 4 5 6; 7 8 9];
diag_elements = diag(A);
sum_diag = sum(diag_elements);
Group C.
2. Write a MATLAB function that takes a 3D array as input and computes the sum of each 2D
slice along the third dimension. Test your function with a 3x3x3 array and display the results.
Ans- Function –
for k = 1:depth
end
end
Function test –
result = sum_slices_3D(array3D);
disp(result);