matrix

python(leetcode)-48旋转图像

点点圈 提交于 2020-02-23 11:07:21
给定一个 n × n 的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 示例 1: 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,3] ]  这道题其实不难懂,有线性代数基础的人都知道转置矩阵,虽然这题与转置不同但是题目意思相似。90度顺时针旋转矩阵。并且要求不能新建矩阵存储即在原矩阵上操作。 虽然题目不难懂,但是做起来非常麻烦,笔者想了很久才做出这题,主要是逻辑上的思路一定要清晰。 先上代码(通过-44ms)击败99% 1 class Solution: 2 def rotate(self, matrix): 3 """ 4 :type matrix: List[List[int]] 5 :rtype: void Do not return anything, modify matrix in-place instead. 6 """ 7 lens=len(matrix[0]) 8 for i in range(lens//2): #对第i行进行换位操作 9 for j in range(i,lens-1-i): #对第j列进行换位操作 10 temp

Applying a matrix to a function [duplicate]

此生再无相见时 提交于 2020-02-23 10:00:12
问题 This question already has answers here : R: matrix by vector multiplication (3 answers) Closed 5 months ago . Trying to apply a matrix to a function, using mapply without success I'm trying to solve a set of equations for different parameters. In a more simplistic form of the set of functions, I'm trying to pass a function to a matrix - constants - a b c [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 and trying to solve the the equation 3*a + 2*b + 3*c and return the answer for each row in the matrix. I

Equivalent of rowsum function for Matrix-class (dgCMatrix)

…衆ロ難τιáo~ 提交于 2020-02-23 07:54:08
问题 For the base R matrix class we have the rowsum function, which is very fast for computing column sums across groups of rows. Is there an equivalent function or approach implemented in the Matrix-package? I'm particularly interested in a fast alternative to rowsum for large dgCMatrix-objects (i.e. millions of rows, but roughly 95% sparse). 回答1: I know this is an old question, but Matrix::rowSums might be the function you are looking for. 来源: https://stackoverflow.com/questions/51025979

剑指offer - 顺时针打印矩阵

只谈情不闲聊 提交于 2020-02-23 03:39:06
题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。 示例 1: 输入:matrix = [[1,2,3],[4,5,6],[7,8,9]] 输出:[1,2,3,6,9,8,7,4,5] 示例 2: 输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] 输出:[1,2,3,4,8,12,11,10,9,5,6,7] 解法一:一圈一圈的数(C++) 我们可以用一个循环来打印矩阵,每一次打印矩阵中的一个圈。现在我们来分析循环结束的条件: 假设这个矩阵的行数是rows,列数是columns。打印第一圈的左上角的坐标是(0,0),第二圈的左上角的坐标是(1,1),以此类推。我们可以选取矩阵中的左上角为(start,start)的一圈作为我们分析的目标。 对一个5*5的矩阵,最后一圈只有一个数字,对应的坐标为(2,2),我们发现5>2*2。对一个6*6的矩阵而言,最后一圈有4个数字,其左上角的坐标仍然为(2,2),我们发现6>2*2依然成立。于是我们可以得出,让循环继续的条件是:columns>start*2并且rows>start*2 我们可以将打印一圈分为下面四步: 第一步:从左到右打印一行 第二步:从上到下打印一列 第三步:从右到左打印一行 第四步:从下到上打印一列 但是并不是每一次都要四步走完

leetcode_48_旋转图像

孤街浪徒 提交于 2020-02-22 05:03:41
旋转图像 描述 中等 给定一个 n × n 的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 示例 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,3] ] 给定 matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10], [13, 3, 6, 7], [15,14,12,16] ], 原地旋转输入矩阵,使其变为: [ [15,13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7,10,11] ] 解题 一开始的想法,numpy里有这个函数,直接mat = np.rot90(mat, -1),就行 这样作弊好像不太好 将矩阵顺时针转90度,只需要将原矩阵上下交换,再对角交换即可 class Solution : def rotate ( self , matrix : List [ List [ int ] ] ) - > None : """ Do not return anything, modify matrix in-place instead. """ n = len ( matrix

Special kind of row-by-row multiplication of 2 sparse matrices in Python

被刻印的时光 ゝ 提交于 2020-02-21 13:47:27
问题 What I'm looking for: a way to implement in Python a special multiplication operation for matrices that happen to be in scipy sparse (csr) format. This is a special kind of multiplication, not matrix multiplication nor Kronecker multiplication nor Hadamard aka pointwise multiplication, and does not seem to have any built-in support in scipy.sparse. The desired operation: Each row of the output should contain the results of every product of the elements of the corresponding rows in the two

Special kind of row-by-row multiplication of 2 sparse matrices in Python

十年热恋 提交于 2020-02-21 13:45:04
问题 What I'm looking for: a way to implement in Python a special multiplication operation for matrices that happen to be in scipy sparse (csr) format. This is a special kind of multiplication, not matrix multiplication nor Kronecker multiplication nor Hadamard aka pointwise multiplication, and does not seem to have any built-in support in scipy.sparse. The desired operation: Each row of the output should contain the results of every product of the elements of the corresponding rows in the two

convert an opencv affine matrix to CGAffineTransform

余生颓废 提交于 2020-02-21 05:57:14
问题 I'd like to take an affine matrix that I have from OpenCV: Mat T = getAffineTransform(src_pt, dst_pt); and then convert it into a CGAffineTransform for use in Core Graphics/Objective-C/iOS. (CGAffineTransform docs) I've tried: CGAffineTransform t = CGAffineTransformIdentity; t.a = T.at<float>(0,0); t.b = T.at<float>(0,1); t.c = T.at<float>(1,0); t.d = T.at<float>(1,1); t.tx = T.at<float>(0,2); t.ty = T.at<float>(1,2); This works fine for x and y translations, but NOT if there is any rotation

Roll pitch and yaw from Rotation matrix with Eigen Library

你。 提交于 2020-02-20 07:44:31
问题 I need to extract the roll pitch yaw angles from a rotation matrix and I want to be sure that what I do is correct. Eigen::Matrix< simFloat, 3, 1> rpy = orientation.toRotationMatrix().eulerAngles(0,1,2); const double r = ((double)rpy(0)); const double p = ((double)rpy(1)); const double y = ((double)rpy(2)); Is that correct? Because I was reading here: http://eigen.tuxfamily.org/dox/group__Geometry__Module.html#gad118fececd448d7485ffea4858775e5a And I was a bit confused when it says, at the

90度旋转矩阵

我的梦境 提交于 2020-02-20 03:54:48
先左右对称反转,再沿右对角线对折,面试记住就行了 代码: 1 class Solution { 2 public: 3 void rotate(vector<vector<int>>& matrix) { 4 int len = matrix.size(); 5 //左右翻 6 for(int i=0;i<len;i++){ 7 for(int j=0;j<len/2;j++){ 8 int temp = matrix[i][j]; 9 matrix[i][j] = matrix[i][len-1-j]; 10 matrix[i][len-1-j] = temp; 11 } 12 } 13 //对角线翻转 14 for(int i=0;i<len;i++){ 15 for(int j=0;j<len-i;j++){ 16 int temp = matrix[i][j]; 17 matrix[i][j] = matrix[len-1-j][len-1-i]; 18 matrix[len-1-j][len-1-i] = temp; 19 } 20 } 21 } 22 }; 来源: https://www.cnblogs.com/FdWzy/p/12334155.html