create a matrix of any size that asks for rows and column. the first row is one and last row is 0 in matlab

You can create a matrix of any size that asks for rows and columns using the following MATLAB code:

main.m
rows = input('Enter number of rows: '); % Ask user to enter the number of rows
cols = input('Enter number of columns: '); % Ask user to enter the number of columns

matrix = zeros(rows, cols); % Initialize a matrix of zeros with the given number of rows and columns

for i = 1:rows
    for j = 1:cols
        if i == 1 % If it's the first row, set the value to 1
            matrix(i, j) = 1;
        elseif i == rows % If it's the last row, set the value to 0
            matrix(i, j) = 0;
        end
    end
end
515 chars
15 lines

This code asks the user to input the number of rows and columns they would like in the matrix. It then initializes a matrix of zeros with the given number of rows and columns. Finally, it uses a nested for loop to iterate through each element in the matrix. If the current iteration is on the first row, it sets the value to 1. If the current iteration is on the last row, it sets the value to 0.

gistlibby LogSnag