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

Here's one way to create a matrix with the second column from the left and second column from the right equal to the column number. We can accomplish this using a nested for loop with conditional logic to set the appropriate values in each column.

main.m
% Define matrix dimensions
n_rows = 5;
n_cols = 4;

% Initialize matrix with zeros
A = zeros(n_rows, n_cols);

% Loop through each column
for j = 1:n_cols
    
    % If this is the second column from the left or right
    if j == 2 || j == n_cols-1
        
        % Loop through each row
        for i = 1:n_rows
            
            % Set the element to the column number
            A(i,j) = j;
            
        end
        
    end
    
end

% Display the resulting matrix
disp(A)
494 chars
28 lines

This will output a matrix where the second column from the left and second column from the right are equal to the column number:

main.m
0    2    0    3
0    2    0    3
0    2    0    3
0    2    0    3
0    2    0    3
85 chars
6 lines

gistlibby LogSnag