matrix

lectcode-旋转图像

孤街浪徒 提交于 2020-02-03 05:48:02
要求 给定一个 n × n 的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 代码 转置加反转 void rotate(vector<vector<int>>& matrix) { int m = matrix.size(); for(int i = 0;i<m;i++) for(int j = i;j<m;j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } for(int i = 0;i<m;i++) for(int j = 0;j<m/2;j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[i][m-j-1]; matrix[i][m-j-1] = temp; } } 总结 第一次for循环用于转置矩阵,第二次for循环用于将该矩阵反转对称。 时间复杂度O(n^2),空间复杂度O(1)。 来源: CSDN 作者: 易殇 链接: https://blog.csdn.net/Dreamer_rx/article/details/104149080

Vectorisation of code for insertion of 2x2 matrix along the diagonal of a large matrix

我与影子孤独终老i 提交于 2020-02-02 12:22:17
问题 I am trying to do an elementwise insertion of a small matrix (2x2) along the diagonal of a large matrix (10x10 say). Overlapping values are added, and the small matrix is only inserted where it can fit fully inside the large matrix. I have achieved this using a for loop, but am curious whether the process can be vectorised. function M = TestDiagonal() N = 10; miniM = [1, -1; -1, 1]; M = zeros(N); for i = 1:N-1 M(i:i+1,i:i+1) = M(i:i+1,i:i+1) + miniM; end end Giving the desired matrix 1 -1 0 0

Vectorisation of code for insertion of 2x2 matrix along the diagonal of a large matrix

落花浮王杯 提交于 2020-02-02 12:20:05
问题 I am trying to do an elementwise insertion of a small matrix (2x2) along the diagonal of a large matrix (10x10 say). Overlapping values are added, and the small matrix is only inserted where it can fit fully inside the large matrix. I have achieved this using a for loop, but am curious whether the process can be vectorised. function M = TestDiagonal() N = 10; miniM = [1, -1; -1, 1]; M = zeros(N); for i = 1:N-1 M(i:i+1,i:i+1) = M(i:i+1,i:i+1) + miniM; end end Giving the desired matrix 1 -1 0 0

R - How to combine 2 pairwise vectors into a matrix

China☆狼群 提交于 2020-02-02 04:40:48
问题 Given the table below: X = col1 col2 col3 row1 "A" "A" "1.0" row2 "A" "B" "0.9" row3 "A" "C" "0.4" row4 "B" "A" "0.9" row5 "B" "B" "1.0" row6 "B" "C" "0.2" row7 "C" "A" "0.4" row8 "C" "B" "0.2" row9 "C" "C" "1.0" Where col3 is a correlation measure between pairs of entities in col1 and col2. How can I construct a matrix for which the column names are col1, the row names are col2, and the values in the cells of the matrix are populated by col3? 回答1: Need some data to work with so I'll make

How to form submatrices with some non-consecutive rows and columns of a matrix

泄露秘密 提交于 2020-02-02 04:10:35
问题 I have a 10 by 10 matrix. I want to form a sub-matrix from this main matrix, using all the rows and columns except the 1st, 2nd and 8th columns and rows. I know Part can be used to form the sub-matrix, but the examples are mostly about forming the sub-matrix using consecutive rows and columns only. 回答1: If this is your matrix: tst = RandomInteger[10, {10, 10}]; This will do the trick for the case at hand: tst[[{3, 4, 5, 6, 7, 9, 10}, {3, 4, 5, 6, 7, 9, 10}]] Instead of explicit list, you

Matrices as function parameters in C89

二次信任 提交于 2020-02-02 02:21:38
问题 For most of my undergrad C programming course, we studied C99 and our lecturer never bothered to teach us the main differences between C99 and previous versions. We have recently been informed that there's a possibility we'll be asked to implement solutions using C89 during our next exam, instead. My question regards the use of variable-length multidimensional arrays with regards to declaration and usage inside a function. In C99, I can have a function like this: void func(int cols, int mat[]

Proper way to cast numpy.matrix to C double pointer

|▌冷眼眸甩不掉的悲伤 提交于 2020-02-01 17:58:49
问题 What is the canonical way of getting a numpy matrix as an argument to a C function which takes a double pointer? Context : I'm using numpy to validate some C code-to wit, I have a C function which takes a const double ** const , and I'm using ctypes to call the .so from Python. I've tried: func.argtypes = ctypeslib.ndpointer(dtype=double, ndim=2, flags="C_CONTIGUOUS") and passed the numpy matrix directly (didn't work), as well as func.argtypes = ctypes.POINTER(ctypes.POINTER(ctypes.c_double))

73:矩阵置零

梦想的初衷 提交于 2020-02-01 05:22:51
问题描述 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。请使用原地算法。 示例 输入 : [ [ 1 , 1 , 1 ] , [ 1 , 0 , 1 ] , [ 1 , 1 , 1 ] ] 输出 : [ [ 1 , 0 , 1 ] , [ 0 , 0 , 0 ] , [ 1 , 0 , 1 ] ] 输入 : [ [ 0 , 1 , 2 , 0 ] , [ 3 , 4 , 5 , 2 ] , [ 1 , 3 , 1 , 5 ] ] 输出 : [ [ 0 , 0 , 0 , 0 ] , [ 0 , 4 , 5 , 0 ] , [ 0 , 3 , 1 , 0 ] ] 题目分析 汗,这题我一开始把原地算法想成了是原地修改。然后想当然的用了2个set记录要变0的行和列,然后让他们变0. 其实人家说的原地算法是利用O(1)的空间复杂度。 我们在思考过程中发现,如果你想用扩展的方式来搞定的话,要解决以下问题: 扩展了行和列之后,你在下次遍历的时候就不清楚这个位置到底是原本就是0,还是被你修改过后是0了。 诚然,对于Python这种语言,你完全可以将初始值为0的值置为False,这样的话我们只需要判断是不是False就可以了。但是对于Java等类型严格的语言来说这题就不可解了吗?不是的。 我们发现这题就是个映射的思想。我们发现某个元素matrix[i][j

Ruby Class#new - Why is `new` a private method?

瘦欲@ 提交于 2020-01-31 19:06:05
问题 I made a Matrix class and I want to use it in various parts of my code. class Matrix def initialize(x, y, v=0) @matrix = Array.new (0..y).each do |j| @matrix[j] = Array.new (0..x).each do |i| @matrix[j][i] = v end end end end When this code is included in the same class as the code that uses it, everything runs fine. When I move this code to lib/matrix.rb and require it, I get the following error: ./phylograph:30:in `block in run': private method `new' called for Matrix:Class (NoMethodError)

Create a matrix using elements of other matrices in MATLAB

99封情书 提交于 2020-01-30 10:15:53
问题 I have two matrices a and b (with equal number of columns). I want to create a third matrix c using a condition: For example, I have: a = [1 2 3 4 1 2 3 4 1 2 3 4; 1 1 1 1 2 2 2 2 3 3 3 3] b = [5 6 7 8 9 10 11 12 13 14 15 16; 17 18 19 20 21 22 23 24 25 26 27 28; 29 30 31 32 33 34 35 36 37 38 39 40] The condition is: a(2, :) == 2 , so the resulting matrix should be: c = [1 2 3 4; 2 2 2 2; 9 10 11 12; 21 22 23 24; 33 34 35 36] 回答1: Try this %With your a and b cols = a(2,:) == 2; c = [a(:,cols)