Intro 1/2 Flashcards

1
Q

Get description of function

A

help + function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

hide response

A

use semicolon
;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

print something

A

disp(“hello”)
A=10;
disp(A)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Make the format tighter

A

format compact

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Get information about a specific variable

A

Example:
var1 = [1,2,3,4];
whos (“var1”)

It lists all the variables in the current workspace, together with information about their size, bytes, class, etc.

–> Output

Name Size Bytes Class Attributes

var1 1x4 32 double

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

example of scalar (zero-dimentional)

A

A=9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

make a row vector

A

my_rowvector = [5 4 12 10 8]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

make a column vector

A

my_columnvector = [1; 2; 14; 5; 6]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Make a matrix

A

M = [1, 23, 15; 6, 7, 11; 0, 16, 8]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

construct special, equally spaced vector (with and without specifying step size)

A

without step size –> E = 5:10

with step size –> F = 0.25:0.5:2 or G = 10:-2:0 or tryout = 1:-0.1:-1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Combine smaller arrays into a larger one (rectangular shape). Create a matrix F from C = [1, 2, 3; 4, 5, 6], D = [7; 8], and E = [9, 0].

A

C = [1, 2, 3; 4, 5, 6];
D = [7; 8];
E = [9, 0];
F = [C, D; E, E];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Add a new row [1, 15, 18] to an existing matrix M

A

M_addition = [1, 15, 18];
M_new = [M; M_addition];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Extract the value in row 4, column 3 of a magic(5) matrix assigned to A.

A

A = magic(5);
value = A(4, 3);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Extract rows 1, 3, and 5 and columns 2 and 4 from a matrix A.

A

submatrix = A([1, 3, 5], [2, 4]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Extract the four values in the bottom right corner of a matrix A.

A

bottom_corner = A([4, 5], [4, 5]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Extract the central 3x3 submatrix from A.

A

central_submatrix = A(2:4, 2:4);

17
Q

Extract all rows of the first column and the last three rows of matrix A.

A

first_column = A(:, 1);
last_three_rows = A(3:5, :);

18
Q

Change values in a matrix A:

Set A(4,4) to 10.
Change row 2, columns 1 to 3 to 5.
Set all values in column 4 to 0.

A

A(4,4) = 10;
A(2,1:3) = 5;
A(:,4) = 0;

19
Q

Add a new column [1, 5, 10, 16, 7] and a new row [6, 7, 80, 9, 3, 5] to matrix A.

A

new_column = [1; 5; 10; 16; 7];
A(:,6) = new_column;

new_row = [6, 7, 80, 9, 3, 5];
A(6,:) = new_row;

20
Q

Create matrices a and b, and form f (stacked vertically) and g (specific columns).

A

a = [9, 12, 13, 0; 10, 3, 6, 15; 2, 5, 10, 3];
b = [1, 4, 2, 11; 9, 8, 16, 7; 12, 5, 0, 3];

f = [a; b];
g = [a(:,1), b(:,4)];

21
Q

Modify matrices:

Set e(2,2) to 20.
Set row 1 of a to all zeros.
Set column 3 of f to numbers 1 through 6.
Replace column 1 of a with column 2 of b.

A

e(2,2) = 20;
a(1,:) = 0;
f(:,3) = 1:6;
a(:,1) = b(:,2);

22
Q

Find the dimensions of a matrix A.

A

dimensions = size(A);

23
Q

Perform scalar addition, subtraction, multiplication, and power operations.

A

Use +, -, *, /, and ^. Example:

matlab
Copy code
result = 2 + 3; % Addition
result = 4^2; % Power

24
Q

When can matrices be added or subtracted?

A

Matrices can only be added or subtracted if they have the same size.

Example:

A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
C = A + B; % Matrix addition

25
Q

Perform element-wise multiplication, division, and power.

A

Use .*, ./, and .^
Example:
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];

C = A .* B; % Element-wise multiplication
D = A ./ B; % Element-wise division
E = A .^ 2; % Element-wise power

26
Q

What is required for matrix multiplication (*)?

A

The number of columns in the first matrix must equal the number of rows in the second.

Example:
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
C = A * B; % Matrix multiplication

27
Q

How do you compute the transpose of a matrix?

A

Use the ‘ operator.
Example:
A = [1, 2; 3, 4];
A_transposed = A’; % Transpose of A

28
Q

Combine matrices vertically and horizontally.

A

Use square brackets.
Example:
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
C = [A; B]; % Vertical stacking
D = [A, B]; % Horizontal stacking

29
Q

Perform scalar operations with matrices.

A

Scalars operate element-wise without needing dots.
Example:
A = [1, 2; 3, 4];
C = 2 * A; % Scalar multiplication
D = A / 2; % Scalar division

30
Q

Why do AA’ and A’A always work, but not A*A?

A

AA’ and A’A have compatible dimensions since the rows and columns align after transposing.

31
Q

Use basic functions like sum, mean, std, max, and min.

A

Example usage:

A = [1, 2; 3, 4];
sum(A, 1); % Column-wise sum
mean(A(:)); % Mean of all elements
max(A, [], 2); % Row-wise max

32
Q

Create matrices with zeros, ones, rand, randn, etc.

A

Z = zeros(3, 4); % 3x4 matrix of zeros
O = ones(3, 4); % 3x4 matrix of ones
R = rand(3, 4); % 3x4 matrix of random numbers

33
Q

Modify and query rows and columns of a matrix.

A

A = [1, 2; 3, 4];
A(:, 1) = [5; 6]; % Change first column
sum(A(2, :)); % Sum of the second row

34
Q

What is the difference between .* and *?

A

.* multiplies elements in the same positions.
* performs matrix multiplication.
Example:
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
C1 = A .* B; % Element-wise
C2 = A * B; % Matrix product

35
Q

Use scalars with matrix operations.

A

Scalars operate element-wise. Example:
A = [1, 2; 3, 4];
C = 2 .* A; % Scalar multiplication (dot optional)
D = A / 2; % Scalar division

36
Q

Perform the following:

Add the sum of the first column of A to the second column of B.
Compute the mean of all elements in B.

A

P = sum(A(:, 1)) + sum(B(:, 2)); % Combine column sums

mean_B = mean(B(:)); % Mean of all elements

37
Q

Modify matrices (e.g., scale a column, add a row).

A

A(:, 3) = 3 * A(:, 3); % Scale the third column

A(3, :) = [5, 6, 7]; % Add a new row