Intro 1/2 Flashcards
(37 cards)
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