create a for loop sure that creates a matrix so that the second column from the left & second column from the right are equal to the column number in matlab

main.m
% Define the size of the matrix
n = 5;

% Preallocate the matrix
A = zeros(n);

% Loop over the rows and columns of the matrix
for i = 1:n
    for j = 1:n
        % Set the second column from the left equal to the column index j
        if j == 2
            A(i,j) = j;
        end
        % Set the second column from the right equal to the column index n-1
        if j == n-1
            A(i,j) = j;
        end
    end
end

% Display the resulting matrix
disp(A)
468 chars
23 lines

This code creates a 5x5 matrix with the second column from the left and second column from the right equal to the respective column numbers. The for loop iterates over the rows and columns of the matrix, and the if statements check if the current column index is equal to 2 (second column from the left) or n-1 (second column from the right). If it is, then the corresponding element of the matrix is set equal to the current column index. Finally, the resulting matrix is displayed using the disp() function.

gistlibby LogSnag