matrix

Creating a Matrix in Python without numpy [duplicate]

断了今生、忘了曾经 提交于 2020-05-26 12:47:27
问题 This question already has answers here : How do you split a list into evenly sized chunks? (63 answers) Closed 3 years ago . I'm trying to create and initialize a matrix. Where I'm having an issue is that each row of my matrix I create is the same, rather than moving through the data set. I've tried to correct it by checking if the value was already in the matrix and that didn't solve my problem. def createMatrix(rowCount, colCount, dataList): mat = [] for i in range (rowCount): rowList = []

Minimize sum of columns in matrix by permutating elements in python

╄→гoц情女王★ 提交于 2020-05-26 07:22:41
问题 I have the following matrix: ([2, 5, 5, 10] [7, 1, 4, 1] [1, 3, 3, 9]) If the columns are summed the result is: [10, 9, 12, 20] My objective is to determine the optimum way to sort the elements in the diferent rows in order to minimize the maximum element in the sum of columns. For example, one possibility would be: ([2, 5, 5, 10] [7, 1, 4, 1] [1, 9, 3, 3]) If the columns are summed the result is: [10, 15, 12, 14] This is a better solution than the first one. The easiest way to do this is

Minimize sum of columns in matrix by permutating elements in python

筅森魡賤 提交于 2020-05-26 07:22:27
问题 I have the following matrix: ([2, 5, 5, 10] [7, 1, 4, 1] [1, 3, 3, 9]) If the columns are summed the result is: [10, 9, 12, 20] My objective is to determine the optimum way to sort the elements in the diferent rows in order to minimize the maximum element in the sum of columns. For example, one possibility would be: ([2, 5, 5, 10] [7, 1, 4, 1] [1, 9, 3, 3]) If the columns are summed the result is: [10, 15, 12, 14] This is a better solution than the first one. The easiest way to do this is

For R Markdown, How do I display a matrix from R variable

喜夏-厌秋 提交于 2020-05-25 06:54:25
问题 I have a rmd document where I have the following ```{r code_block, echo=FALSE} A = matrix(c(1,3,0,1),2,2) B = matrix(c(5,3,1,4),2,2) ``` $$ \begin{bmatrix} 1 & 0 \\ 3 & 1 \\ \end{bmatrix} * \begin{bmatrix} 5 & 1 \\ 3 & 4 \\ \end{bmatrix} $$ Now I would like to instead of hard coding the LaTeX part manually, I could use the matrix from the variables A and B instead. How could this be done? Thanks. 回答1: Straightforwardly, you can write latex line. writeLines() or cat() would be helpful. You can

Finding neighbor cells in a grid with the same value. Ideas how to improve this function?

删除回忆录丶 提交于 2020-05-21 07:31:07
问题 I am new to Python (learning it for a little over 1 month) and I tried creating Tic Tac Toe. However once I finished it, I decide to expand the board (from 3x3 to 9x9 depending from the customer input) and allow a win by connecting 4 in a row, column or diagonal anywhere in the board. Therefore I needed a function that search - depending from the customer input - in every directions on the board, without going overboard, for 3 connected cells with the same mark. Then I realize that actually I

Multiplying Matrices in One-Dimensional Arrays

爷,独闯天下 提交于 2020-05-18 19:57:27
问题 void multiply(int a[], int row1, int col1, int b[], int row2, int col2) { int d[size]; for (int i = 0; i < row1; i++) { for (int j = 0; j < col2; j++) { int sum = 0.0; for (int k = 0; k < col2; k++) sum = sum + a[i * col1 + k] * b[k * col2 + j]; d[i * col2 + j] = sum; } } for (int i = 0; i < size; i++) { if (i % col2 == 0) { printf("\n"); } printf("%d ", d[i]); } } I have this as a function to multiple two one-dimensional arrays that are supposed to be matrices. I'm using an online compiler

Cumulative dot product with numpy

[亡魂溺海] 提交于 2020-05-16 04:01:09
问题 I have an ndarray A, populated with N squared DxD matrices (shape (N,D,D)). I want to transform it into an ndarray B of the same shape, where B[0]=A[0] and for every i>0, B[i] = np.dot(B[i-1], A[i]). While a basic implementation is obvious, I wondered whether this operation has a faster implementation than a for loop. Let me, For example, describe another way to perform the calculation: B[0...N/2] = compute for A[0]...A[N/2 - 1] the basic way B[N/2...N] = compute for A[N/2]...A[N] the basic

Is K-means for clustering data with many zero values?

落花浮王杯 提交于 2020-05-15 18:38:49
问题 I need to cluster a matrix which contains mostly zeros values...Is K-means appropriate for these kind of data or do I need to consider a different algorithm? 回答1: No. The reason is that the mean is not sensible on sparse data. The resulting mean vectors will have very different characteristics than your actual data; they will often end up being more similar to each other than to actual documents! There are some modifications that improve k-means for sparse data such as spherical k-means. But

Is K-means for clustering data with many zero values?

白昼怎懂夜的黑 提交于 2020-05-15 18:38:10
问题 I need to cluster a matrix which contains mostly zeros values...Is K-means appropriate for these kind of data or do I need to consider a different algorithm? 回答1: No. The reason is that the mean is not sensible on sparse data. The resulting mean vectors will have very different characteristics than your actual data; they will often end up being more similar to each other than to actual documents! There are some modifications that improve k-means for sparse data such as spherical k-means. But

Python: Find a word in a matrix of characters

陌路散爱 提交于 2020-05-15 04:57:06
问题 I am trying to create a word game that involves finding words in a matrix of 5x5 characters, like so: [['a', 'a', 'u', 'r', 'a'], ['m', 'v', 'g', 'n', 'x'], ['a', 'q', 'h', 'y', 'o'], ['p', 'r', 'h', 'l', 'h'], ['v', 'h', 'y', 'o', 'j']] which I have represented as a list of lists. "xylon" should be found but not "nylon" since this reuses the 'n'. I found a similar problem here but I do not know C. My current solution involves creating a dictionary for each letter in the word, consisting of a