I want to generate a random binary matrix, so I’m using W=np.random.binomial(1, p, (n,n)). It works fine, but I want a constraint that no row is just of 0s. I create the following function: It also works fine, but it seems to me too inefficient. Is there a faster way to create this constraint? Answer When the matrix is large,

# Tag: matrix

## How to make recursive function hold it’s original parameter value?

I’m trying to write a function that calculates the determinant of a square matrix using recursion. The parameter of the oldest function – the first one called – changes to the matrix returned by scale_down() and I don’t know why. I tried substituting “matrix” with “var” in order not to use it directly, but it amounted to nothing. The code

## pandas – creating new rows of combination with value of 0

I have a pandas dataframe like user_id music_id has_rating A a 1 B b 1 and I would like to automatically add new rows for each of user_id & music_id for those users haven’t rated, like user_id music_id has_rating A a 1 A b 0 B a 0 B b 1 for each of user_id and music_id combination pairs those

## Generating 2D matrix in python with all the elements is 0

I’m making a game in python. The map of the game is a stored as matrix with number of line and column are equal, the matrix look kind of like this: I want to make a matrix generator to store it in ram when the game is openned in stead of store the whole matrix like that in the game

## How to put number of a particular element in a particular row and column constraints in a matrix?

I have to basically create a symmetric matrix of NxN. Which has 1s and 0s randomly populated into it. However the only constraint is I need only one ‘1’ in any row and any column. I wrote a code to generate the matrix but it has more than one ‘1’ in any row or column. I need to follow the

## Python, plot matrix diagonal elements

I have an 120×70 matrix of which I want to graph diagonal lines. for ease of typing here, I will explain my problem with a smaller 4×4 matrix. index 2020 2021 2022 2023 0 1 2 5 7 1 3 5 8 10 0 1 2 5 3 1 3 5 8 4 I now want to graph for example

## string to complex matrix representation in python

I have the following example string: s =”1 1+in1-i 0″ Now I have to turn this string into a complex matrix. I am aware of the np.matrix() function but it is not designed for a complex matrix. Maybe some of you can provide me some ideas of how I can go forward. I also tried to split at n but

## is it possible to generate a list in list for desired numbers?

I wanted to generate a list in list : now, I have basically two options, either I input the list through a text file or I should generate the list by itself. is it possible to generate this type of list by itself using nested loops? I wanted to put -1 at the place of the middle zero of each

## Changing graph dataset matrices from sparse format to dense

I am trying to use the CoRA dataset to train a graph neural network on tensorflow for the first time. The features and adjacency matrices provided by the dataset comes in a sparse representation but I don’t need it here. Thus, I want to use numpy’s todense() but it turns out it doesn’t exist. For your reference, here is the

## Scale/resize a square matrix into a larger size whilst retaining the grid structure/pattern (Python)

Like above, I want to retain the same ‘grid’ format whilst increasing the dimensions of the 2D array. How would I go about doing this? I assume the original matrix can only be scaled up by an integer n. Answer You can use numba if performance is of importance (similar post) with no python jitting and in parallel mode if