In Matlab we can use the following code lines in order to set the entire last row and last column of 1+N x 1+N sized matrix x as 1.
x=zeros((1+N,1+N))
x(1+N,1:N+1) = 1
x(1:N+1,1+N) = 1
What would be the equivalent way to get the similar result in python with and without using numpy? Thanks!
To create an array full of zeros:
x = numpy.zeros((1+N,1+N))To set one row or column to 1: e.g.,
x[:,3] = 1Without using numpy, presumably you’d use nested lists. The easiest way to get a rectangular array of zeros is with a comprehension like this:
x = [m*[0] for i in range(n)]; then you can replace one row ofxby sayingx[3] = ...but updating a whole column, or some but not all of a row, will require an explicit loop.