matrix

[leetcode] Spiral Matrix II

て烟熏妆下的殇ゞ 提交于 2021-02-10 16:29:11
Given an integer n , generate a square matrix filled with elements from 1 to n 2 in spiral order. For example, Given n =3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] https://oj.leetcode.com/problems/spiral-matrix-ii/ 思路:按要求输出,注意边界。 public class Solution { public int[][] generateMatrix(int n) { int[][] mat = new int[n][n]; if (n <= 0) return mat; int num = 1; int left = 0; int right = n - 1; int top = 0; int bottom = n - 1; while (num <= n * n) { int i; for (i = left; i <= right; i++) mat[top][i] = num++; for (i = top + 1; i <= bottom; i++) mat[i][right] =

Find the nXn submatrix with the highest number of 1's

谁说胖子不能爱 提交于 2021-02-10 14:53:22
问题 I tried to solve this question but without success: Given 3n X 3n boolean matrix, find the nXn submatrix with the highest number of 1's in O(n^2). I had an idea but it was O(n^3). The idea: count the 1's in the matrix begin with (0,0) and move to the right and check only the new col that should be added VS the col that should be deleted. And the same idea for down. Each submatrix calculation is O(n^2) and passing all the matrix is O(n) so it's too much. I don't see a way how to pass across

Return Similarity Matrix From Two Variable-length Arrays of Strings (scipy option?)

大兔子大兔子 提交于 2021-02-10 12:29:06
问题 Say I have two arrays: import numpy as np arr1 = np.array(['faucet', 'faucets', 'bath', 'parts', 'bathroom']) arr2 = np.array(['faucett', 'faucetd', 'bth', 'kichen']) and I want to compute the similarity of the strings in arr2 to the strings in arr1 . arr1 is an array of correctly spelled words. arr2 is an array of words not recognized in a dictionary of words. I want to return a matrix which will then be turned into a pandas DataFrame. My current solution (credit): from scipy.spatial

How do I implement the Laplace expansion algorithm in c?

瘦欲@ 提交于 2021-02-10 07:21:54
问题 I'm having trouble figuring out a way to make this algorithm work, as I can't figure out how to do the middle part of the problem. Here's my code so far: int det(int matrixSize, int matrix[][matrixSize]){ int determinant = 0, matrixValues[matrixSize * matrixSize], matrixFirstRowValues[matrixSize * matrixSize]; for(int i = matrixSize; i > 2; i--){ for(int row = 0; row < matrixSize; row++){ for(int col = 0; col < matrixSize; col++){ matrixFirstRowValues[row + (matrixSize - i)] = matrix[1][col +

Fill an matrix with the following structure in c

核能气质少年 提交于 2021-02-10 06:42:26
问题 I have the following structures. typedef struct arr_integer { int size; int *arr; }arr_arr_integer; arr_arr_integer alloc_arr_integer(int len) { arr_arr_integer a = {len, len > 0 ? malloc(sizeof(int) * len) : NULL}; return a; } What I intend to do is fill the matrix with the above structures. But I don't know how to manipulate the structure to fill the matrix. Tried it this way, but I get an error. int main(int argc, char const *argv[]) { int len,rows,columns; scanf("%d",&rows); scanf("%d",

How to extract the value from a certain position in a matrix in Haskell?

烈酒焚心 提交于 2021-02-10 05:40:30
问题 I have to implement a gameboard for 2048. I declared it: type Board = [[Int]] In order to add a new random value in a random cell I must check the value in that cell, but I don't know how to get the value. I've seen different examples using a monad, but I don't know how to use Monad and I was wondering if there is another way to do this. Can anyone help me with an example? Thanks! 回答1: Well, as for checking the value in a particular cell – that's just list-indexing, you can simply use !! .

Normals transformation for Back-Face culling

别说谁变了你拦得住时间么 提交于 2021-02-10 05:09:54
问题 Starting from this lesson here: WebGL 3D Perspective I am triyng to implement Back-Face culling no magic. I am computing on the fly the face normals in object space. After that, I am setting the fudgeFactor inside m[2][3] to get the perspective divide by Z . To check if the shear matrix works, I expanded my snippet using directly the vertex positions projected out of the WebGL vertex shader, and added the "projected position" flag. Now, I am triyng to use the z-component inside the

Normals transformation for Back-Face culling

别说谁变了你拦得住时间么 提交于 2021-02-10 05:08:19
问题 Starting from this lesson here: WebGL 3D Perspective I am triyng to implement Back-Face culling no magic. I am computing on the fly the face normals in object space. After that, I am setting the fudgeFactor inside m[2][3] to get the perspective divide by Z . To check if the shear matrix works, I expanded my snippet using directly the vertex positions projected out of the WebGL vertex shader, and added the "projected position" flag. Now, I am triyng to use the z-component inside the

Find the largest values on a matrix in R

社会主义新天地 提交于 2021-02-10 03:11:46
问题 I am working on a matrix in R, 230 x 230 and I want to extract the 10 (or any other number than 1) max inputs on the matrix, both their position and value. The extra problem is that this is a similarity matrix, so I have 1s in the diagonal which of course I want to leave out of the max search. Any ideas or commands for that? 回答1: A neat way to do this in general is with the underused arrayInd function, which gives you row and column positions for plain jane vector positions. That's how which(

Find the largest values on a matrix in R

泄露秘密 提交于 2021-02-10 03:07:23
问题 I am working on a matrix in R, 230 x 230 and I want to extract the 10 (or any other number than 1) max inputs on the matrix, both their position and value. The extra problem is that this is a similarity matrix, so I have 1s in the diagonal which of course I want to leave out of the max search. Any ideas or commands for that? 回答1: A neat way to do this in general is with the underused arrayInd function, which gives you row and column positions for plain jane vector positions. That's how which(