how do i create a matrix that is 5x5, has eye(3) and then columns 4 and 5 have 2. also the last 2 rows of the matrix are 3s in matlab

To create a matrix that is 5x5, with the first three columns being an identity matrix (eye(3)), and the fourth and fifth columns both containing the value 2, you can use the following code in MATLAB:

main.m
% Create a 5x5 matrix
matrix = zeros(5, 5);

% Fill the first three columns with eye(3)
matrix(1:3, 1:3) = eye(3);

% Fill the fourth and fifth columns with 2
matrix(:, 4:5) = 2;

% Display the matrix
matrix
208 chars
12 lines

This will give you the following matrix:

main.m
1     0     0     2     2
0     1     0     2     2
0     0     1     2     2
0     0     0     2     2
0     0     0     2     2
130 chars
6 lines

To make the last two rows of the matrix contain the value 3, you can modify the code as follows:

main.m
% Create a 5x5 matrix
matrix = zeros(5, 5);

% Fill the first three columns with eye(3)
matrix(1:3, 1:3) = eye(3);

% Fill the fourth and fifth columns with 2
matrix(:, 4:5) = 2;

% Fill the last two rows with 3
matrix(4:5, :) = 3;

% Display the matrix
matrix
261 chars
15 lines

This will give you the following matrix:

main.m
1     0     0     2     2
0     1     0     2     2
0     0     1     2     2
3     3     3     3     3
3     3     3     3     3
130 chars
6 lines

gistlibby LogSnag