matrix

Avoid division by zero between matrices in MATLAB [duplicate]

懵懂的女人 提交于 2020-01-24 08:41:18
问题 This question already has an answer here : Matlab element-wise division by zero (1 answer) Closed 3 years ago . I'm using matlab and I have two matrices : G = 1 1 1 1 1 1 1 1 and the scond: m = 4 4 4 4 0 0 0 0 I want this result : x = 1/4 1/4 1/4 1/4 0 0 0 0 What I did so far is this : x = G ./ m But it returns : x = 1/4 1/4 1/4 1/4 NaN NaN NaN NaN How can I avoid the divison by zero by placing a default value "0" if there is a division by zero ? 回答1: You can convert the NaNs back to zero: x

Avoid division by zero between matrices in MATLAB [duplicate]

橙三吉。 提交于 2020-01-24 08:41:16
问题 This question already has an answer here : Matlab element-wise division by zero (1 answer) Closed 3 years ago . I'm using matlab and I have two matrices : G = 1 1 1 1 1 1 1 1 and the scond: m = 4 4 4 4 0 0 0 0 I want this result : x = 1/4 1/4 1/4 1/4 0 0 0 0 What I did so far is this : x = G ./ m But it returns : x = 1/4 1/4 1/4 1/4 NaN NaN NaN NaN How can I avoid the divison by zero by placing a default value "0" if there is a division by zero ? 回答1: You can convert the NaNs back to zero: x

How to update opengl modelview matrix with my own 4x4 matrix?

二次信任 提交于 2020-01-24 02:41:11
问题 I have 4x4 matrix for object's transformations. float mat44[16]; But i don't know how to update OpenGL ModelView matrix using my matrix. should i use glTranslatef()/glRotatef() with relavant values from my matrix or should i use glLoadMatrix(),glMultMatrix() ? Pls help. Thanks. 回答1: If you want to apply your transformation to current transformation already in OpenGL matrix stack, then you should write: glMultMatrixf(mat44); But if you want to discard what's currently on top of OpenGL matrix

Fast slicing and multiplication of scipy sparse CSR matrix

北慕城南 提交于 2020-01-24 01:46:48
问题 I have a scipy sparse CSR matrix of size 2M x 50k with 200M non-zero values (100 per row). I need to slice 120k rows of it by a (randomly distributed) index (which is a pandas Series ) and then multiply that submatrix by a sparse vector of size 1x50k (with 100 non-zero values as well). I do this: slice = matrix[index.tolist(), :] result = slice.dot(vector.T).T.toarray()[0] # returns 1x120k array The slicing takes 0.7s (slow) and then multiplication takes 0.05s . Instead, I can multiply the

How to convert coordinates on Bitmap to real coordiates on Image View displayed on screen

大兔子大兔子 提交于 2020-01-24 01:08:31
问题 I have an Image View which displays an image (e.g 2000x1000 pixels) and I have a coordinate (X,Y) on that image (not the image view). The canvas of my Image View is 600x800 for example. How can I convert the point (X,Y) to screen coordinate so that I can draw a path with them on the OnDraw(...) method of Image View. Any help is appreciated! Thank you. Update: If I use matrix to draw the path between coordinates, it works but the path and objects i draw become really small. Here is the code i

Modifying block matrices in Python

☆樱花仙子☆ 提交于 2020-01-24 00:21:46
问题 I would like to take a matrix and modify blocks of it. For example, with a 4x4 matrix the {1,2},{1,2} block is to the top left quadrant ([0,1;4,5] below). The {4,1},{4,1} block is the top left quadrant if we rearrange the matrix so the 4th row/column is in position 1 and the 1st in position 2. Let's made such a 4x4 matrix: a = np.arange(16).reshape(4, 4) print(a) ## [[ 0 1 2 3] ## [ 4 5 6 7] ## [ 8 9 10 11] ## [12 13 14 15]] Now one way of selecting the block, where I specify which rows

How R subtracts a matrix from an integer

元气小坏坏 提交于 2020-01-24 00:00:10
问题 This a question about R's internals. I am curious if someone could explain how the following call works. # Let's just work with part of the iris data data(iris) df <- iris[1:10, 1:4] # Now the question 1 - df Does R create another matrix of equivalent dimensions? Does it loop over all elements? How is R subtracting a matrix from an integer? 回答1: Note that your example is a data.frame and not a matrix. I will refer to the data.frame case. An S3 method is dispatched by the Ops group generic

Trying to find the number of diagonals of 1 in each 3x3 tile in a 15x15 binary matrix?

不问归期 提交于 2020-01-23 21:43:34
问题 I am trying to find the count of diagonal 1s in each 3x3 tile e.g. 0 0 1 1 0 0 0 1 0 0 1 0 1 0 0 or 0 0 1 from the below 15x15 matrix. set.seed(99) mat <- matrix(sample(c(0,1), 225, prob=c(0.8,0.2), replace=TRUE), nrow=15) print(mat) [,1][,2][,3][,4][,5][,6][,7][,8][,9][,10][,11][,12][,13][,14][,15] [1,] 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 [2,] 0 1 0 1 0 0 1 0 0 0 1 0 0 0 1 [3,] 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 [4,] 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 [5,] 0 0 0 0 1 0 0 1 1 1 0 0 0 0 0 [6,] 0 0 0 0 0 0 1 0

leetcode 【高级】Kth Smallest Element in a Sorted Matrix Java

我的梦境 提交于 2020-01-23 18:32:36
题干 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素。 请注意,它是排序后的第k小元素,而不是第k个元素。 示例: matrix = [ [ 1 , 5 , 9 ] , [ 10 , 11 , 13 ] , [ 12 , 13 , 15 ] ] , k = 8 , 返回 13 。 说明: 你可以假设 k 的值永远是有效的, 1 ≤ k ≤ n2 。 想法 最小堆,遍历每一层的数然后放到最小堆 堆顶弹出 接着遍历第二行 第k次弹出就是 2.非常规的二分法 若采用Binary Search, 先猜一个值,这个值是由最小值matrix[0][0]和最大值matrix[n-1][n-1]求mid得来. 然后算下小于等于这个mid的matrix元素个数,若是个数小于k, 就在mid到最大值之间再猜一个新的mid. 直到l 不再小于 r. Time Complexity: O(nlog(matrix[n-1][n-1] - matrix[0][0])), 每次lowerThanMidCount用时O(n). Space: O(1). Java代码 public class Solution { public int kthSmallest ( int [ ] [ ] matrix , int k ) { int n = matrix . length ;

Constructing a Multidimensional Differentiation Matrix

元气小坏坏 提交于 2020-01-23 17:56:05
问题 I have been trying to construct the matrix D ij , defined as I want to plot it for points located at x i = -cos[ π (2 i + 1) / (2 N )] on the interval [-1,1] to consequentially take derivatives of a function. I am though having problems constructing the differentiating matrix D ij . I have written a python script as: import numpy as np N = 100 x = np.linspace(-1,1,N-1) for i in range(0, N - 1): x[i] = -np.cos(np.pi*(2*i + 1)/2*N) def Dmatrix(x,N): m_ij = np.zeros(3) for k in range(len(x)):