matrix

螺旋矩阵

独自空忆成欢 提交于 2020-03-12 04:10:49
问题描述: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] 输出: [1,2,3,6,9,8,7,4,5] 示例 2: 输入: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12] ] 输出: [1,2,3,4,8,12,11,10,9,5,6,7] 问题分析: 旋转操作必存在方向的转变。对于一个二维数组(矩阵)而言,顺时针旋转只存在四次方向的改变(右下左上)。假设我们的初始方向是向右,当不能继续向右时,我们就要将方向改变为向下(因为是顺时针旋转),当不能继续向下前时,改变方向为向左,以此类推。。。 在这里,说明一下什么情况下我们就不能按照原方向访问元素。当我们找到的下一个坐标越界或者下一个坐标的元素已经被访问过,此时,我们就不能按照原方向访问了。 因此,我们需要一个标志数组,记录矩阵中的每一个元素是否被访问过。 代码实现: class Solution { public List < Integer > spiralOrder ( int [ ] [ ] matrix ) { List ans = new ArrayList ( ) ; if ( matrix . length == 0

c++封装Matrix之美

孤人 提交于 2020-03-10 20:29:27
在游戏引擎开发当中,数学知识是最基础的,也是最底层的,现在通过c++进行底层Matrix简单的封装, 代码如下: #ifndef MATRIX_H #define MATRIX_H #include <cstring> #include <cassert> #include "Vector.hpp" class Matrix { public: Matrix(); Matrix( const Matrix & other ); explicit Matrix( int rows, int cols ); explicit Matrix( int rows, int cols, float * data ); ~Matrix(); const float * operator[]( int index ) const; float * operator[]( int index ); Matrix & operator=( const Matrix & other ); Matrix operator+( const Matrix & other ) const; Matrix operator-( const Matrix & other ) const; Matrix operator*( const Matrix & other ) const; Matrix operator

chapter_1 : 方程组的几何解释

て烟熏妆下的殇ゞ 提交于 2020-03-09 00:45:47
方程组的几何解释 row picture column picture matrix 例子1: 方程组 2 x − y = 0 2x -y = 0 2 x − y = 0 − x + 2 y = 0 -x + 2y = 0 − x + 2 y = 0 系数矩阵 [ 2 − 1 − 1 2 ] [ x y ] = [ 0 0 ] \left[\begin{matrix} 2 & -1 \\ -1 & 2 \end{matrix}\right] \left[\begin{matrix}x \\ y \end{matrix} \right] = \left[\begin{matrix}0 \\ 0 \end{matrix} \right] [ 2 − 1 ​ − 1 2 ​ ] [ x y ​ ] = [ 0 0 ​ ] A = [ 2 − 1 − 1 2 ] , x = [ x y ] , b = [ 0 0 ] A = \left[\begin{matrix} 2 & -1 \\ -1 & 2 \end{matrix}\right], \pmb x = \left[\begin{matrix}x \\ y \end{matrix} \right],\pmb b = \left[\begin{matrix}0 \\ 0 \end{matrix} \right] A = [ 2 − 1

leetcode菜狗入门 | 54. 螺旋矩阵

岁酱吖の 提交于 2020-03-08 19:36:43
螺旋矩阵 题目描述 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。 示例 1: 输入 : [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] 输出 : [ 1 , 2 , 3 , 6 , 9 , 8 , 7 , 4 , 5 ] 示例 2: 输入 : [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] ] 输出 : [ 1 , 2 , 3 , 4 , 8 , 12 , 11 , 10 , 9 , 5 , 6 , 7 ] 解题思路 确定每次行走路线的上下左右边界,每次走完一个方向更新方向,如果各边的关系不满足上大于下,左小于右,break 代码 class Solution { public : vector < int > spiralOrder ( vector < vector < int >> & matrix ) { if ( matrix . empty ( ) || matrix [ 0 ] . empty ( ) ) return { } ; vector < int > ans ; int m = matrix . size ( ) , n = matrix [ 0 ] . size ( )

Eigen子矩阵操作

冷暖自知 提交于 2020-03-07 07:06:33
1 子矩阵操作简介 子矩阵操作又称块操作,在矩阵运算中,子矩阵的提取和操作应用也十分广泛。因此Eigen中也提供了相关操作的方法。提取的子矩阵在操作过程中既可以用作左值也可以用作右值。 2 块操作的一般使用方法 在Eigen中最基本的快操作运算是用 .block() 完成的。提取的子矩阵同样分为动态大小和固定大小。 块操作 构建动态大小子矩阵 提取块大小为(p,q),起始于(i,j) matrix.block(i,j,p,q) 同样需要注意的是在Eigen中,索引是从0开始。所有的操作方法都可以适用于Array.同样使用固定大小的操作方式在小型矩阵运算时更加的快,但要求在编译时就要知道矩阵的大小。 下面是一个使用示例: #include <iostream> #include "Eigen/Dense" using namespace std; using namespace Eigen; int main() { MatrixXf m(4,4); m<< 1,2,3,4, 5,6,7,8, 9,10,11,12, 13,14,15,16; cout<<"Block in the middle"<<endl; cout<<m.block<2,2>(1,1)<<endl<<endl; for(int i = 1;i <= 3;++i) { cout<<"Block of size "

椭圆隐式方程和参数方程的互相转换

[亡魂溺海] 提交于 2020-03-06 05:25:33
1. 隐式方程转参数方程 二次曲线的一般方程为: A x 2 + 2 B x y + C y 2 + 2 D x 2 + 2 E y 2 + F = 0. A{x^2}+2Bxy+C{y^2}+2Dx^2+2Ey^2+F=0. A x 2 + 2 B x y + C y 2 + 2 D x 2 + 2 E y 2 + F = 0 . 若 B 2 − A C < 0 B^2-AC<0 B 2 − A C < 0 , 为椭圆; B 2 − A C = 0 B^2-AC=0 B 2 − A C = 0 , 为抛物线; B 2 − A C > 0 B^2-AC>0 B 2 − A C > 0 ,为双曲线。 二次曲线可通过旋转和平移来变成标准方程,从而得到其几何参数。旋转的作用是消去交叉项,平移的作用是使中心为原点 ,下面以椭圆为例。 方程的二次项为: [ x , y ] [ A B B C ] [ x , y ] , (1) [x, y] \left[ \begin{matrix} A &B\\ B &C \end{matrix} \right] [x, y], \tag{1} [ x , y ] [ A B ​ B C ​ ] [ x , y ] , ( 1 ) 一次项为 2 [ x , y ] [ D E ] . 2[x, y] \left[ \begin{matrix} D\\ E

find value in array according to the row and column number

风流意气都作罢 提交于 2020-03-06 04:54:28
问题 I have two matrix A and B,the first row of matrix A(1,:)=[1 2] refer to the number of row and column matrix B(1,2)=21,now I want to do this work for another rows of matrix A without loops? A=[1 2;2 3;1 3;3 3]; B=[1 21 34;45 65 87;4 55 66]; for i=1:4 d(i,:)=B(A(i,1),A(i,2)) end d =[21; 87;34;66] 回答1: An alternative to sub2ind is d = B(A(:,1)+ (A(:,2)-1)*size(B,1)); 回答2: Use sub2ind to get linear indices of the required values of B and then use these indices to retrieve those values. d = B

C - matrix rotation

六眼飞鱼酱① 提交于 2020-03-06 03:50:43
问题 I'm writing a program in C and I need a M x N matrix to rotate clockwise. I tried some algorithms, but they only work in N x N matrices. The matrix {(1,4), (2,5), (3,6)} should become {(3,2,1), (6,5,4)} : 1 4 2 5 -> 3 2 1 3 6 6 5 4 I wrote this piece of code to transpose the matrix, and now I don't know how to swap the columns: void transpose(int matrix[MAX][MAX], int m, int n) { int transpose[MAX][MAX], d, c; for (c = 0; c < m; c++) for( d = 0 ; d < n ; d++) transpose[d][c] = matrix[c][d];

python实现克莱姆法则

拟墨画扇 提交于 2020-03-05 20:25:55
文章目录 首先完成python模拟行列式运算 公式分析 模块分析与实现 环境 模块导入 全排列 逆序数 方阵计算 克莱姆法则 *Cramer's rule* 注:本文对numpy对象使用append方法时均使用了深拷贝deepcopy,因为python中对象的赋值是按引用传递的,如果不使用深拷贝在append时会改变原有对象从而覆盖原先的值 首先完成python模拟行列式运算 ∣ a 11 a 12 ⋯ a 1 n a 21 a 22 ⋯ a 2 n ⋮ ⋮ ⋱ ⋮ a n 1 a n 2 ⋯ a n n ∣ = ∑ ( − 1 ) t a 1 p 1 a 2 p 2 ⋯ a n p n \begin{vmatrix} {a_{11}}&{a_{12}}&{\cdots}&{a_1n}\\ {a_{21}}&{a_{22}}&{\cdots}&{a_2n}\\ {\vdots}&{\vdots}&{\ddots}&{\vdots}\\ {a_{n1}}&{a_{n2}}&{\cdots}&{a_{nn}}\\ \end{vmatrix}= \sum{(-1)^{t}}{a_{1p_{1}}a_{2p_{2}}}{\cdots}{a_{np_{n}}} ∣ ∣ ∣ ∣ ∣ ∣ ∣ ∣ ∣ ​ a 1 1 ​ a 2 1 ​ ⋮ a n 1 ​ ​ a 1 2 ​ a 2 2 ​ ⋮

Speeding up calculation of symmetric matrices; use of outer

ε祈祈猫儿з 提交于 2020-03-05 06:55:12
问题 I need to speed up a calculation that produces a symmetric matrix. Currently I have something like this: X <- 1:50 Y<- 1:50 M <- outer(X, Y, FUN = myfun) where myfun is a quite complicated, vectorized, but symmetrical function (myfun(x, y) = myfun(y, x)). So my code unnecessarily wastes time calculating the lower triangular matrix as well as the upper triangular matrix. How can I avoid that duplication without using slow for-loops? 回答1: If your function is slow and timing scales with size of