Intro 1/2 Flashcards
Get description of function
help + function
hide response
use semicolon
;
print something
disp(“hello”)
A=10;
disp(A)
Make the format tighter
format compact
Get information about a specific variable
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
example of scalar (zero-dimentional)
A=9
make a row vector
my_rowvector = [5 4 12 10 8]
make a column vector
my_columnvector = [1; 2; 14; 5; 6]
Make a matrix
M = [1, 23, 15; 6, 7, 11; 0, 16, 8]
construct special, equally spaced vector (with and without specifying step size)
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
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].
C = [1, 2, 3; 4, 5, 6];
D = [7; 8];
E = [9, 0];
F = [C, D; E, E];
Add a new row [1, 15, 18] to an existing matrix M
M_addition = [1, 15, 18];
M_new = [M; M_addition];
Extract the value in row 4, column 3 of a magic(5) matrix assigned to A.
A = magic(5);
value = A(4, 3);
Extract rows 1, 3, and 5 and columns 2 and 4 from a matrix A.
submatrix = A([1, 3, 5], [2, 4]);
Extract the four values in the bottom right corner of a matrix A.
bottom_corner = A([4, 5], [4, 5]);
Extract the central 3x3 submatrix from A.
central_submatrix = A(2:4, 2:4);
Extract all rows of the first column and the last three rows of matrix A.
first_column = A(:, 1);
last_three_rows = A(3:5, :);
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(4,4) = 10;
A(2,1:3) = 5;
A(:,4) = 0;
Add a new column [1, 5, 10, 16, 7] and a new row [6, 7, 80, 9, 3, 5] to matrix A.
new_column = [1; 5; 10; 16; 7];
A(:,6) = new_column;
new_row = [6, 7, 80, 9, 3, 5];
A(6,:) = new_row;
Create matrices a and b, and form f (stacked vertically) and g (specific columns).
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)];
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.
e(2,2) = 20;
a(1,:) = 0;
f(:,3) = 1:6;
a(:,1) = b(:,2);
Find the dimensions of a matrix A.
dimensions = size(A);
Perform scalar addition, subtraction, multiplication, and power operations.
Use +, -, *, /, and ^. Example:
matlab
Copy code
result = 2 + 3; % Addition
result = 4^2; % Power
When can matrices be added or subtracted?
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
Perform element-wise multiplication, division, and power.
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
What is required for matrix multiplication (*)?
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
How do you compute the transpose of a matrix?
Use the ‘ operator.
Example:
A = [1, 2; 3, 4];
A_transposed = A’; % Transpose of A
Combine matrices vertically and horizontally.
Use square brackets.
Example:
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
C = [A; B]; % Vertical stacking
D = [A, B]; % Horizontal stacking
Perform scalar operations with matrices.
Scalars operate element-wise without needing dots.
Example:
A = [1, 2; 3, 4];
C = 2 * A; % Scalar multiplication
D = A / 2; % Scalar division
Why do AA’ and A’A always work, but not A*A?
AA’ and A’A have compatible dimensions since the rows and columns align after transposing.
Use basic functions like sum, mean, std, max, and min.
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
Create matrices with zeros, ones, rand, randn, etc.
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
Modify and query rows and columns of a matrix.
A = [1, 2; 3, 4];
A(:, 1) = [5; 6]; % Change first column
sum(A(2, :)); % Sum of the second row
What is the difference between .* and *?
.* 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
Use scalars with matrix operations.
Scalars operate element-wise. Example:
A = [1, 2; 3, 4];
C = 2 .* A; % Scalar multiplication (dot optional)
D = A / 2; % Scalar division
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.
P = sum(A(:, 1)) + sum(B(:, 2)); % Combine column sums
mean_B = mean(B(:)); % Mean of all elements
Modify matrices (e.g., scale a column, add a row).
A(:, 3) = 3 * A(:, 3); % Scale the third column
A(3, :) = [5, 6, 7]; % Add a new row