matrix

plotting two matrices in the same graph with matplotlib

杀马特。学长 韩版系。学妹 提交于 2021-01-27 18:00:27
问题 I want to plot two matrices in the same graph. These matrices have the shape 3x5. They were created using meshgrid for two arrays of the size 3 & 5 (a is size 3, b is size 5). The entries of the matrices were calculated using the values from the arrays, which I want to show in the plot, so e.g. if M1 was calculated with the entries a1 and b1, M1 should be shown where the two indices meet in the diagram. Also, the axis of the diagram should be labeled with the entries of the two arrays. To

byrow= TRUE argument in matrix function for R

无人久伴 提交于 2021-01-27 15:16:56
问题 I read in somewhere that when creating a matrix, R stores the elements of the matrix in a vector as a column major format with additional information about the matrix dimensions. If matrix(1:6, nrow = 3, ncol = 2) R internally stores values as a vector of 1:6 . However, if we set byrow = TRUE , does it mean R is storing values as c(1, 3, 5, 2, 4, 6) ? 回答1: The byrow = TRUE tells R that it needs to manipulate the input to get it to the column-major order. So yes, matrix(1:6, nrow = 3, byrow =

Add submatrices at certain locations

被刻印的时光 ゝ 提交于 2021-01-27 15:01:14
问题 I have a test matrix (z) of shape 40x40, filled with zeros. I need to add 4 submatrices of shapes, called c1, c2(5x5), c3(7x7) and c4(9x9) at specific locations to the test matrix. I want to place the submatrices centers at the respective locations, then simply perform addition of elements. The locations in the test matrix are: z(9,9), z(9,29), z(29,9), z(29,29). I tried looking at these threads, but I cannot get a clear answer on how resolve my problem. How to add different arrays from the

Non-Conformable Arrays when Doing Matrix Multiplication in R

爷,独闯天下 提交于 2021-01-27 13:24:25
问题 I am trying to implement Kernel Ridge Regression in R. The formula is: alpha <- ((lambda.I + K)^(-1)) * y Lambda = 0.1. I = identity matrix the same size as K. y is a feature vector that has the same number of rows as K. So I tried this in R: I <- diag(nrow(df_matrix) lambda <- 0.1 alpha <- (lambda * I + df_matrix) ^ (-1) * df_vector I get the following error Error in (0.1 * I + df_matrix)^(-1) * df_vector : non-conformable arrays Here's some information on my dataset > nrow(df_matrix) [1]

MATLAB - Create psudorandom sparse matrix

你。 提交于 2021-01-27 10:52:04
问题 Is there an easy way of making a 'random' sparse matrix with a specific number of nonzero entries? Here is my attempt: r = randperm(n,m) % n = size of matrix, m = number of nonzeros in each column H = sparse(r, r,1,n,n); But the matrix H doesn't have exactly m nonzeros in each column. For example if I use this to make a 100 x 100 matrix with 10 nonzeros in each column only 10 columns have exactly 10 1's in them. I'm sure there's an easy way to do this but I can't see it. 回答1: This will

MATLAB - Create psudorandom sparse matrix

て烟熏妆下的殇ゞ 提交于 2021-01-27 10:48:20
问题 Is there an easy way of making a 'random' sparse matrix with a specific number of nonzero entries? Here is my attempt: r = randperm(n,m) % n = size of matrix, m = number of nonzeros in each column H = sparse(r, r,1,n,n); But the matrix H doesn't have exactly m nonzeros in each column. For example if I use this to make a 100 x 100 matrix with 10 nonzeros in each column only 10 columns have exactly 10 1's in them. I'm sure there's an easy way to do this but I can't see it. 回答1: This will

Create matrix with same in and out degree for all nodes

邮差的信 提交于 2021-01-27 10:36:17
问题 I've stated this question in graph theory terms, but that conceptualization isn't necessary. What I'm trying to do, using Python, is produce a matrix of zeros and ones, where every row has the same number of ones and every column has the same number of ones. The number for rows will not be the same as the number for columns when the number of rows (sending nodes) does not equal the number of columns (receiving nodes) -- which is something I'm allowing. It makes sense to me to do this in numpy

How to generate random matrix without repetition in rows and cols?

放肆的年华 提交于 2021-01-27 07:35:06
问题 How to generate random matrix without repetition in rows and cols with specific range example (3x3): range 1 to 3 2 1 3 3 2 1 1 3 2 example (4x4): range 1 to 4 4 1 3 2 1 3 2 4 3 2 4 1 2 4 1 3 回答1: This algorithm will do the trick, assuming you want to contain all elements between 1 and n %// Elements to be contained, but no zero allowed a = [1 2 3 4]; %// all possible permutations and its size n = numel(a); %// initialization output = zeros(1,n); ii = 1; while ii <= n; %// random permuation

What is the fastest way to compute a sparse Gram matrix in Python?

人盡茶涼 提交于 2021-01-27 07:23:30
问题 A Gram matrix is a matrix of the structure X @ X.T which of course is symmetrical. When dealing with dense matrices, the numpy.dot product implementation is intelligent enough to recognize the self-multiplication to exploit the symmetry and thus speed up the computations (see this). However, no such effect can be observed when using scipy.sparse matrices: random.seed(0) X = random.randn(5,50) X[X < 1.5] = 0 X = scipy.sparse.csr_matrix(X) print(f'sparsity of X: {100 * (1 - X.count_nonzero() /

Average every N rows by column

这一生的挚爱 提交于 2021-01-27 07:23:00
问题 I have four matrices that are 240x30. I need to calculate an average for every 15 rows and I need to do this by column. So, in the end I should have 30 columns with 16 values. So, for instance: myMatrix = randi(240,30) And this is what I have so far: averageBins = 15; meanByBinsMyMatrix = arrayfun(@(i) mean(myMatrix (i:i+averageBins-1)),1:averageBins:length(myMatrix )-averageBins+1)'; % the averaged vectormean() This seems to be working but I think it only does the job for the first column.