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.m306 chars16 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