matrix

How can I control the formatting when saving a matrix to a file?

╄→尐↘猪︶ㄣ 提交于 2019-12-30 10:47:11
问题 I save a matrix to a file like this: save(filepath, 'mtrx', '-ascii'); Is there a way to tell MATLAB to write 0 instead of 0.0000000e+000 values? It would be nice because it would be faster and easier to see which values differ from zero. 回答1: I suggest using DLMWRITE instead of SAVE since you're dealing with ASCII files. It will give you more control over the formatting. For example, you could create an output file delimited by spaces with a field width of 10 and 6 digits after the decimal

Fill a matrix from a matrix of indices

匆匆过客 提交于 2019-12-30 09:46:47
问题 I want to fill a matrix from an array of indices : import numpy as np indx = [[0,1,2],[1,2,4],[0,1,3],[2,3,4],[0,3,4]] x = np.zeros((5,5)) for i in range(5): x[i,indx[i]] = 1. The result is : array([[ 1., 1., 1., 0., 0.], [ 0., 1., 1., 0., 1.], [ 1., 1., 0., 1., 0.], [ 0., 0., 1., 1., 1.], [ 1., 0., 0., 1., 1.]]) As desired. Question Is there a way to do this in pure python/numpy without looping ? 回答1: Use advanced-indexing after intialization - x[np.arange(len(indx))[:,None], indx] = 1 来源:

Get maximum value from all matrices in a list

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-30 09:45:47
问题 I've created a List of matrices, and now I want to get the maximum values of row in all matrices, how do I get them? Here is the code for the list: i <- 1 tryList <- list() treeList <- list() accList <- list() for(t_mtry in 1:40){ for(t_ntree in 20:300{ rf <- randomForest(class ~., data=training, mtry=t_mtry, ntree=t_ntree) tbl <- table(predicted = predict(rf,evalSet,type="class"),actual=evalSet$class) #get the accuracy of the classification as a list retVal <- accuracy(tbl) tryList <- c

Most efficient way to construct similarity matrix

落爺英雄遲暮 提交于 2019-12-30 08:53:28
问题 I'm using the following links to create a "Euclidean Similarity Matrix" (that I convert to a DataFrame). https://stats.stackexchange.com/questions/53068/euclidean-distance-score-and-similarity http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.spatial.distance.euclidean.html The way I'm doing it is an iterative approach which works but it takes a while when the datasets are big. The pandas pd.DataFrame.corr() is really fast and useful for pearson correlations. How can I perform

Most efficient way to construct similarity matrix

百般思念 提交于 2019-12-30 08:52:06
问题 I'm using the following links to create a "Euclidean Similarity Matrix" (that I convert to a DataFrame). https://stats.stackexchange.com/questions/53068/euclidean-distance-score-and-similarity http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.spatial.distance.euclidean.html The way I'm doing it is an iterative approach which works but it takes a while when the datasets are big. The pandas pd.DataFrame.corr() is really fast and useful for pearson correlations. How can I perform

Most efficient way to construct similarity matrix

时间秒杀一切 提交于 2019-12-30 08:52:01
问题 I'm using the following links to create a "Euclidean Similarity Matrix" (that I convert to a DataFrame). https://stats.stackexchange.com/questions/53068/euclidean-distance-score-and-similarity http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.spatial.distance.euclidean.html The way I'm doing it is an iterative approach which works but it takes a while when the datasets are big. The pandas pd.DataFrame.corr() is really fast and useful for pearson correlations. How can I perform

Projecting a 3D point to 2D screen space using a perspective camera matrix

a 夏天 提交于 2019-12-30 08:38:11
问题 I am attempting to project a series of 3D points onto the screen using a perspective camera matrix. I do not have world space (or consider it being an identity matrix) and my camera does not have camera space (or consider it an identity matrix), I do have a 4x4 matrix for my object space. I am taking the object matrix and multiplying it by the camera perspective matrix, generated with the following method: Matrix4 createPerspectiveMatrix( Float fov, Float aspect, Float near, Float far ) {

Add a diagonal of zeros to a matrix in MATLAB

十年热恋 提交于 2019-12-30 08:25:12
问题 Suppose I have a matrix A of dimension Nx(N-1) in MATLAB, e.g. N=5; A=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16; 17 18 19 20 ]; I want to transform A into an NxN matrix B , just by adding a zero diagonal, i.e., B=[ 0 1 2 3 4; 5 0 6 7 8; 9 10 0 11 12; 13 14 15 0 16; 17 18 19 20 0]; This code does what I want: B_temp = zeros(N,N); B_temp(1,:) = [0 A(1,:)]; B_temp(N,:) = [A(N,:) 0]; for j=2:N-1 B_temp(j,:)= [A(j,1:j-1) 0 A(j,j:end)]; end B = B_temp; Could you suggest an efficient way to

How to merge 3 matrices into 1 in opencv?

只谈情不闲聊 提交于 2019-12-30 07:18:07
问题 I have three matrices, each of size 4x1 . I want to copy all of these matrices to another matrix of size 4x3 and call it R . Is there a smart way to do it? 回答1: You can just use hconcat for horizontal concatenation. You can use it per matrix, e.g. hconcat( mat1, mat2, R ), or apply it directly on a vector or array of matrices. Here's a sample code: vector<Mat> matrices = { Mat(4, 1, CV_8UC1, Scalar(1)), Mat(4, 1, CV_8UC1, Scalar(2)), Mat(4, 1, CV_8UC1, Scalar(3)), }; Mat R; hconcat( matrices,

Calculating EigenVectors in C# using Advanced Matrix Library in C#. NET

半世苍凉 提交于 2019-12-30 07:06:07
问题 Ok guys, I am using the following library: http://www.codeproject.com/KB/recipes/AdvancedMatrixLibrary.aspx And I wish to calculate the eigenvectors of certain matrices I have. I do not know how to formulate the code. So far I have attempted: Matrix MatrixName = new Matrix(n, n); Matrix vector = new Matrix(n, 0); Matrix values = new Matrix(n, 0); Matrix.Eigen(MatrixName[n, n], values, vector); However it says that the best overloaded method match has some invalid arguments. I know the library