← Back to quizzesFree quiz

MATLAB Fundamentals Quiz

Welcome to this comprehensive guide on MATLAB fundamentals. Whether you are new to the environment or need a quick refresher, this course covers the essential commands, functions, and syntax…

21 questions~11 min
MATLAB Fundamentals Quiz — Qwi
0 / 21
Score: 0%
1

Which command clears all variables from the current workspace?

2

If a script needs to display the value of variable x without a newline, which function should be used?

3

A user wants to create a 3‑by‑4 matrix of ones. Which command produces the correct matrix?

4

What is the result of executing A = [1 2;3 4]; size(A) in MATLAB?

5

Which operator performs element‑wise multiplication of two matrices A and B?

6

A script contains the line "x = sqrt(16);". What value is stored in x after execution?

7

Which command loads variables from a file named data.mat into the workspace?

8

When plotting multiple curves on the same figure, which command must be issued before the second plot command?

9

What does the command "axis([0 10 -5 5])" accomplish?

10

Which function returns the eigenvalues of matrix M?

11

A user writes "A = [1 2 3]; B = [4 5 6]; C = [A; B];". What is the size of C?

12

Which command will display the list of variables currently stored in the workspace?

13

What is the effect of the command "clc" in a MATLAB session?

14

Which function creates a linearly spaced vector from 0 to 1 with 5 points?

15

A matrix A has size 4×4 and rank 2. Which statement about the linear system Ax = b is always true?

16

Which command saves the current workspace variables to a file named "mydata.mat"?

17

When solving Ax = b using the backslash operator, which expression is correct?

18

What does the function "eig(A)" return when A is a square matrix?

19

Which of the following statements about the "plot" function is FALSE?

20

A user writes "A = [1 2; 3 4]; B = inv(A); C = A*B;". What is matrix C?

21

Which command adds a title "Signal Plot" to the current figure?

MATLAB Fundamentals: Core Concepts and Commands

Welcome to this comprehensive guide on MATLAB fundamentals. Whether you are new to the environment or need a quick refresher, this course covers the essential commands, functions, and syntax you need to work efficiently in MATLAB. The material is organized around common quiz questions, turning each query into a learning module that explains the underlying concept, provides examples, and highlights best practices for programming in MATLAB.

1. Managing the Workspace: Clearing Variables

In MATLAB, the workspace holds all variables created during a session. To start fresh or free memory, you often need to clear variables.

  • Command: clear
  • Purpose: Removes all variables from the current workspace.
  • Typical usage: clear (clears everything) or clear varName (clears a specific variable).

Note that clc only clears the command window, not the variables, and who merely lists them.

2. Displaying Output Without a Newline

When you need precise control over the format of displayed text, MATLAB offers the fprintf function.

  • Command: fprintf('Value: %g', x);
  • Difference from disp: disp automatically appends a newline after the output, whereas fprintf does not unless you include \n in the format string.
  • Example: x = 5; fprintf('x = %d', x); prints x = 5 on the same line as any preceding text.

3. Creating Matrices of Specific Values

MATLAB provides a family of functions to generate matrices filled with particular values. To create a matrix of ones, use the ones function.

  • Command: M = ones(3,4);
  • Result: A 3‑by‑4 matrix where every element equals 1.
            1 1 1 1
            1 1 1 1
            1 1 1 1
          
  • Related functions: zeros (creates a matrix of zeros), eye (creates an identity matrix), and rand (creates a matrix of random numbers between 0 and 1).

4. Determining Matrix Dimensions with size

The size function returns the dimensions of an array. For a 2‑by‑2 matrix A = [1 2; 3 4];, the call size(A) yields 2 2, indicating two rows and two columns.

  • Syntax: dim = size(A); returns a vector [rows columns].
  • Alternative usage: rows = size(A,1); columns = size(A,2); extracts each dimension separately.

5. Element‑wise Operations

MATLAB distinguishes between matrix algebra and element‑wise arithmetic. To multiply corresponding elements of two matrices of the same size, use the dot‑asterisk operator .*.

  • Example:
            A = [1 2; 3 4];
            B = [5 6; 7 8];
            C = A .* B;   % C = [5 12; 21 32]
          
  • Common pitfalls: Using * attempts matrix multiplication, which requires compatible inner dimensions, while .* works element‑by‑element regardless of linear algebra rules.

6. Basic Mathematical Functions: sqrt

The sqrt function computes the square root of a numeric value. Executing x = sqrt(16); stores the value 4 in x.

  • Other useful functions: abs (absolute value), log (natural logarithm), exp (exponential), and power (or the ^ operator) for exponentiation.

7. Loading Data from .mat Files

MATLAB stores variables in binary .mat files. To bring those variables into the current workspace, use the load command.

  • Command: load('data.mat');
  • Result: All variables saved in data.mat become available as regular workspace variables.
  • Selective loading: load('data.mat', 'var1', 'var2'); loads only the specified variables.

8. Plotting Multiple Curves on a Single Figure

When visualizing several data sets together, you must tell MATLAB to retain the current plot before adding new graphics. The hold on command accomplishes this.

  • Typical workflow:
            x = 0:0.1:2*pi;
            plot(x, sin(x), 'b');   % First curve (blue sine)
            hold on;                % Keep the figure active
            plot(x, cos(x), 'r');   % Second curve (red cosine)
            hold off;               % Optional: release the hold
          
  • Alternative: Use subplot to create separate axes within the same figure, but hold on is the command that directly overlays multiple plots.

9. Quick Reference Cheat Sheet

Below is a concise table of the commands discussed, ideal for a quick lookup while coding.

  • Clear workspace: clear
  • Print without newline: fprintf
  • Create matrix of ones: ones(m,n)
  • Check size: size(A)
  • Element‑wise multiplication: A .* B
  • Square root: sqrt(x)
  • Load .mat file: load('file.mat')
  • Overlay plots: hold on

10. Best Practices for Writing Clean MATLAB Code

To become proficient, adopt habits that improve readability and performance:

  • Use descriptive variable names: Avoid single letters unless they are loop indices.
  • Pre‑allocate arrays: Functions like zeros or ones allocate memory up front, reducing execution time.
  • Comment liberally: Use % for inline comments and %{ ... %} for block comments.
  • Vectorize operations: Whenever possible, replace loops with vectorized expressions (e.g., A .* B instead of a for loop).
  • Clear unused variables: Periodically run clear or clearvars to free memory in long scripts.

By mastering these fundamental commands and adhering to clean coding practices, you will be well‑equipped to tackle more advanced MATLAB projects, from data analysis to algorithm development.