We create matrices to organize numbers or data in rows and columns. This helps us work with many values at once, like a table.
Matrix creation in MATLAB
A = [1 2 3; 4 5 6; 7 8 9];
Use square brackets [ ] to create a matrix.
Separate elements in the same row with spaces or commas.
Use semicolons ; to start a new row.
A = [1 2 3; 4 5 6; 7 8 9];
B = [10, 20; 30, 40];
C = zeros(2,3);
D = eye(3);This program creates three matrices: a 3x3 matrix A, a 2x3 zero matrix B, and a 3x3 identity matrix C. It then displays each matrix.
A = [1 2 3; 4 5 6; 7 8 9]; disp('Matrix A:'); disp(A); B = zeros(2,3); disp('Matrix B (zeros):'); disp(B); C = eye(3); disp('Matrix C (identity):'); disp(C);
You can create matrices with different sizes by changing the number of rows and columns.
Use functions like zeros, ones, and eye to quickly create special matrices.
Remember that MATLAB indexes matrices starting at 1, not 0.
Matrices are made using square brackets with spaces or commas for columns and semicolons for rows.
Special functions help create common matrices like zeros and identity matrices.
Matrices let you organize and work with many numbers easily.
Practice
Solution
Step 1: Understand matrix dimensions
A 2x3 matrix has 2 rows and 3 columns.Step 2: Check each option's shape
[1 2 3; 4 5 6]has 2 rows separated by semicolon and 3 columns separated by spaces, so it is 2x3.Final Answer:
[1 2 3; 4 5 6] -> Option CQuick Check:
2 rows and 3 columns = [1 2 3; 4 5 6] [OK]
- Using commas instead of semicolons for rows
- Mismatched number of columns in rows
- Confusing rows and columns
Solution
Step 1: Recall identity matrix function
In MATLAB,eye(n)creates an n-by-n identity matrix.Step 2: Check options
eye(3)useseye(3), which is the correct function for a 3x3 identity matrix.Final Answer:
eye(3) -> Option AQuick Check:
Identity matrix = eye(3) [OK]
- Using zeros or ones instead of eye
- Using diag with incorrect vector size
- Confusing identity with zero matrix
A = [1 2; 3 4];
B = zeros(2,3);
C = [A B];
disp(C)
Solution
Step 1: Understand matrix sizes
A is 2x2, B is 2x3 of zeros.Step 2: Concatenate horizontally
[A B] joins matrices side by side, resulting in 2 rows and 5 columns.Final Answer:
[1 2 0 0 0; 3 4 0 0 0] -> Option BQuick Check:
Horizontal concat of 2x2 and 2x3 = 2x5 matrix [OK]
- Trying to concat matrices with different row counts vertically
- Confusing zeros size
- Expecting error instead of valid concat
M = [1 2 3; 4 5];
Solution
Step 1: Check matrix row sizes
First row has 3 elements, second row has 2 elements.Step 2: Understand MATLAB matrix rules
All rows must have the same number of columns; here they differ.Final Answer:
Rows have different number of columns -> Option DQuick Check:
Matrix rows must match column count [OK]
- Thinking commas are required between rows
- Using parentheses instead of brackets
- Ignoring unequal row lengths
Solution
Step 1: Create a 4x4 matrix of 0.5
Using0.5*ones(4)sets all elements to 0.5.Step 2: Replace diagonal elements with 1
Using logical indexingM(eye(4) == 1) = 1;sets diagonal to 1.Final Answer:
M = 0.5*ones(4); M(eye(4) == 1) = 1; -> Option AQuick Check:
Diagonal 1, others 0.5 =M = 0.5*ones(4); M(eye(4) == 1) = 1;[OK]
- Adding eye(4) instead of replacing diagonal
- Mixing addition and multiplication incorrectly
- Setting diagonal to 0.5 instead of 1
Ready to prove it?
Pass the test and this pattern moves unknown
