create a matrix making sure that the second column from the left & second column from the right are equal to the column number in matlab

You can create such matrix in MATLAB by using indexing, loops and conditional statements. The following code creates a matrix of size m x n such that the second column from the left and the second column from the right is equal to the column number.

main.m
% define matrix size
m = 5; % number of rows
n = 7; % number of columns

% create matrix
A = zeros(m, n);

% loop through columns
for j = 1:n
    % check if column is second from left or right
    if j == 2 || j == n-1
        % set values of column based on column number
        A(:, j) = j;
    end
end
306 chars
16 lines

In this code, we first define the size of the matrix m x n and create a matrix A of size m x n filled with zeros. We then loop through each column of the matrix A using the for loop. We check if the current column is the second column from the left or the second column from the right using the conditional statement if j == 2 || j == n-1.

If the current column is the second column from the left or the second column from the right, we set the values of the column j to be equal to the column number j using the indexing notation A(:, j) = j.

After the loop is complete, the matrix A will have the second column from the left and the second column from the right equal to the column number.

gistlibby LogSnag