matrix

Make numpy matrix more sparse

断了今生、忘了曾经 提交于 2019-12-31 03:10:49
问题 Suppose I have a numpy array np.array([ [3, 0, 5, 3, 0, 1], [0, 1, 2, 1, 5, 2], [4, 3, 5, 3, 1, 4], [2, 5, 2, 5, 3, 1], [0, 1, 2, 1, 5, 2], ]) Now, I want to randomly replace some elements with 0. So that I have an output like this np.array([ [3, 0, 0, 3, 0, 1], [0, 1, 2, 0, 5, 2], [0, 3, 0, 3, 1, 0], [2, 0, 2, 5, 0, 1], [0, 0, 2, 0, 5, 0], ]) 回答1: We can use np.random.choice(..., replace=False) to randomly select a number of unique non-zero flattened indices and then simply index and reset

Replacing matrix elements indexed by another matrix

流过昼夜 提交于 2019-12-31 03:02:14
问题 After several hours of searching, I am turning to your expertise. Beginner in R, I try to speed up my code. My goal is to replace the values in a matrix A . However, I want to replace values based on two vectors of another matrix B . B[, 1] is the name of row i of the matrix A . The second column, B[, 2] corresponds to the name of column of the matrix A . The first version of my code was to use the match function in a loop. for(k in 1:L){ i <- B[k,1] j <- B[k,2] d <- match(i,rownames(A)) e <-

How to vectorize row-wise diagonalization of a matrix

谁说我不能喝 提交于 2019-12-31 02:55:28
问题 I have an n-by-m matrix that I want to convert to a mn-by-m matrix, with each m-by-m block of the result containing the diagonal of each row. For example, if the input is: [1 2; 3 4; 5 6] the output should be: [1 0; 0 2; 3 0; 0 4; 5 0; 0 6] Of course, I don't want to assemble the matrix step by step myself with a for loop. Is there a vectorized and simple way to achieve this? 回答1: For a vectorized way to do this, create the linear indices of the diagonal elements into the resulting matrix,

OpenGL Rotations around World Origin when they should be around Local Origin

守給你的承諾、 提交于 2019-12-31 02:17:09
问题 I'm implementing a simple camera system in OpenGL. I set up gluPerspective under the projection matrix and then use gluLookAt on the ModelView matrix. After this I have my main render loop which checks for keyboard events and, if any of the arrow keys are pressed, modifies angular and forward speeds (I only rotate through the y axis and move through the z (forwards)). Then I move the view using the following code (deltaTime is the amount of time since the last frame was rendered in seconds,

Idiomatic matrix type conversion, say convert Integer (0/1) matrix to boolean matrix

微笑、不失礼 提交于 2019-12-31 02:00:34
问题 I have: B <- matrix(c(1, 0, 0, 1, 1, 1), nrow=3, ncol=2) and I want: B [,1] [,2] [1,] TRUE TRUE [2,] FALSE TRUE [3,] FALSE TRUE as.logical(B) gives a 1D vector. Is there an idiomatic way to do the matrix type conversion? I'm currently doing the wordy: boolVector <- as.logical(B) m <- nrow(B) n <- ncol(B) m <- matrix(boolVector, m , n) m 回答1: mode(B) <- "logical" or "mode<-"(B, "logical") . We can also use storage.mode function. This workaround is good for two reasons: the code is much

Change matrix dimension

為{幸葍}努か 提交于 2019-12-31 01:52:16
问题 Let's make a replicable example: This is my initial matrix d<- matrix(1:80,,5) d [,1] [,2] [,3] [,4] [,5] [1,] 1 17 33 49 65 [2,] 2 18 34 50 66 [3,] 3 19 35 51 67 [4,] 4 20 36 52 68 [5,] 5 21 37 53 69 [6,] 6 22 38 54 70 [7,] 7 23 39 55 71 [8,] 8 24 40 56 72 [9,] 9 25 41 57 73 [10,] 10 26 42 58 74 [11,] 11 27 43 59 75 [12,] 12 28 44 60 76 [13,] 13 29 45 61 77 [14,] 14 30 46 62 78 [15,] 15 31 47 63 79 [16,] 16 32 48 64 80 I would like to reshape my matrix cutting every colomn by 4 and taking

Matlab: Matrix Neighbour Extraction

回眸只為那壹抹淺笑 提交于 2019-12-31 01:47:11
问题 I have a large number of images which I've broken down into segments such that their matrices look like: img = [ 1 1 1 1 1 2 2 2 3 3 3 3 1 1 1 1 2 2 2 2 2 3 3 3 1 1 1 4 4 4 2 2 2 3 3 3 5 5 5 5 5 5 5 2 2 3 3 3 ]; where each number represents a different region and each region is arbitrarily shaped. So in this case, region 1 has neighbours 2, 4 and 5, region 2 has neighbours 1, 3 and 4 and so on. I've extracted all of the regions into separate cells and obtained statistics (mean, variance, etc)

How to delete zeros from matrix in MATLAB?

こ雲淡風輕ζ 提交于 2019-12-31 01:25:09
问题 Here is my problem: I have a nxn matrix in matlab. I want to delete all the zeros of this matrix and put the rows of it in vectors. For n=4 , let say I have the following matrix: A = [ 1 1 0 0 1 2 0 0 1 0 0 0 1 2 1 0 ]; How to get the following: v1 = [ 1 1 ]; v2 = [ 1 2 ]; v3 = [ 1 ]; v4 = [ 1 2 1 ]; I did the following: for i = 1:size(A, 1) tmp = A(i, :); tmp(A(i, :)==0)=[]; v{i} = tmp; end 回答1: Slightly faster than Divakar's answer: nzv = arrayfun(@(n) nonzeros(A(n,:)), 1:size(A,1),

Repeat matrix n-times into a list

天涯浪子 提交于 2019-12-30 23:49:29
问题 I have a matrix that i want to duplicate n times in a list. Obviously the rep() function does not work on matrices, so does anyone have a good suggestion how to make this better than my code below? Thanks! # Create sample matrix jwprox <- matrix(ncol=15,nrow=15) # Create list of n-times matrices jwprox <- list(jwprox,jwprox,jwprox) 回答1: You can use either lapply() n <- 3 x <- lapply(seq_len(n), function(X) jwprox) str(x) # List of 3 # $ : logi [1:15, 1:15] NA NA NA NA NA NA ... # $ : logi [1

How to create NxM Corr matrix from Column 2 of 2xN signals in R?

感情迁移 提交于 2019-12-30 14:46:00
问题 I have 2x N amount of 1D Signals in files where Column 1 is Signal 1 and Column 2 Signal 2. Code 1 is simplified example about 1x N amount of 1D signals, while Code 2 is the actual target with two pieces of pseudocode about: to create two dimensional vector ( files[[i]] = i,i+1 ) - just two integer data units in each row separated by comma, and and then accessing the data there later ( tcrossprod( files[[]][, 2], files[[]][, 2] ) ) where I cannot refer to all columns 2 of all signals