matrix

C++ Memory allocation. Matrix

筅森魡賤 提交于 2020-01-03 04:52:13
问题 I looked into two different method to allocate memory for the elements of a matrix Method n.1 int** matrix = new int*[rows]; for (int i = 0; i < rows; ++i) matrix[i] = new int[cols]; Method n.2 int** matrix = new int*[rows]; if (rows) { matrix[0] = new int[rows * cols]; for (int i = 1; i < rows; ++i) matrix[i] = matrix[0] + i * cols; } I can figure out what Method n.1 does, but I can't figure out what exactly is supposed to do the if clause in Method n.2 (I would implement it without and it

Jacobi/Gauss Seidel Methods in Matlab

大城市里の小女人 提交于 2020-01-03 04:52:04
问题 I need to implement the Jacobi and Guass Seidel, methods in Matlab. I found this link which has code that produces correct results (on the one sample I tried) for each. See post 3. My problem is that the implementation is different to that described here, and here I'm happy enough using someone else's code (indeed I prefer something tried and tested) but I want to understand exactly how it works. Can someone point me to a description of the implementations used in the post? Alternately are

R convert Datatable distinct column values to column names and column values as values from another column [duplicate]

眉间皱痕 提交于 2020-01-03 04:45:05
问题 This question already has answers here : How to reshape data from long to wide format (11 answers) Closed 3 years ago . Question 1) I have an R data table with three columns (The actual dataset is bigger but simplifying for better understanding) Column_One, Column_Two, Column_Three A, 1, 4 A, 2, 3 A, 3, 77 B, 1, 44 B, 2, 32 B, 3, 770 C, 1, 43 C, 2, 310 C, 3, 68 I want to create a new matrix (data table) from the above as shown below. A, B, C 4, 44, 43 3, 32, 310 77, 770, 68 Please note in the

Getting two diagonals of a Matrix

独自空忆成欢 提交于 2020-01-03 03:35:11
问题 I'm using this Matrix class: http://ruby-doc.org/stdlib-2.0.0/libdoc/matrix/rdoc/Matrix.html I'm instantiating it this way: @matrix = Matrix.build(3,3) { |row, col| Cell.new } How can I get the elements from both diagonals? I found I can get the elements from the upper left to the lower right diagonal this way: @matrix.each(:diagonal).to_a But I cant find a way to get the elements in the upper right to lower left diagonal. 回答1: Diagonal: @matrix.square? && (0...@matrix.row_size).map { |i|

How do I calculate the covariance matrix without any built-in functions or loops in MATLAB?

梦想的初衷 提交于 2020-01-03 03:35:09
问题 Is it possible to find the covariance of a matrix without using any built-in functions or loops in MATLAB? I'm completely clueless about the idea of solving this problem. I was thinking of something like: cov(x,y) = 1/(n-1) .* (x*y) However, I don't think this will work. Any ideas? 回答1: Here's a great example of how to numerically compute the covariance matrix. http://www.itl.nist.gov/div898/handbook/pmc/section5/pmc541.htm. However, let's put this in this post for the sake of completeness. I

How to use dissimilarity matrix with function metaMDS?

℡╲_俬逩灬. 提交于 2020-01-03 03:23:07
问题 I have a matrix derived from a table with three original columns: column 1 = site codes, column 2 = species codes and column 3 = biomass weight for each species. The biomass weight of each species in each plot is displayed in the matrix. The matrix can be calculated with one of the three following options (thanks to feedback on an earlier question): reshape::cast(dissimBiom, plot ~ species, value = 'biomass', fun = mean) by(dissimBiom, dissimBiom$biomass, function(x) with(x, table(plot,

How to create the matrix from data frame?

倖福魔咒の 提交于 2020-01-03 03:08:26
问题 i have a data frame as follows, I want to create a matrix, where the average sleep duration hour (SLP) is shown according to 3 recruiting site(site) and 5 recruiting year(year). SLP site year 8.6 1 2008 7.2 1 2005 6.4 2 2006 9.5 3 2007 6.1 2 2009 3.6 1 2005 8.6 1 2008 7.2 1 2005 6.4 2 2006 9.5 3 2007 6.1 2 2009 5.1 3 2008 2.1 2 2006 My desired output is: 1 2 3 2005 6.00 - - 2006 - 4.97 - 2007 - - 9.5 2008 8.60 - 5.1 2009 - 6.10 - Column name is variable of site, row name is variable of year

How to convert matrix into array?

倖福魔咒の 提交于 2020-01-03 02:52:07
问题 I have a large data set and i want to convert it into array. But for instance lets have a simple example. I have following matrix `\begin{table}[ht] \begin{center} \begin{tabular}{rrrrrrr} \hline & 1 & 2 & 3 & 4 & 5 & 6 \\ \hline 1 & 1 & 11 & 21 & 31 & 41 & 51 \\ 2 & 2 & 12 & 22 & 32 & 42 & 52 \\ 3 & 3 & 13 & 23 & 33 & 43 & 53 \\ 4 & 4 & 14 & 24 & 34 & 44 & 54 \\ 5 & 5 & 15 & 25 & 35 & 45 & 55 \\ 6 & 6 & 16 & 26 & 36 & 46 & 56 \\ 7 & 7 & 17 & 27 & 37 & 47 & 57 \\ 8 & 8 & 18 & 28 & 38 & 48 &

Matlab: how can I perform row operations without brute-force for loop?

不羁的心 提交于 2020-01-03 02:27:15
问题 I need to do function that works like this : N1 = size(X,1); N2 = size(Xtrain,1); Dist = zeros(N1,N2); for i=1:N1 for j=1:N2 Dist(i,j)=D-sum(X(i,:)==Xtrain(j,:)); end end (X and Xtrain are sparse logical matrixes) It works fine and passes the tests, but I believe it's not very optimal and well-written solution. How can I improve that function using some built Matlab functions? I'm absolutely new to Matlab, so I don't know if there really is an opportunity to make it better somehow. 回答1: You

Rotate point around vector 3d

丶灬走出姿态 提交于 2020-01-03 02:00:10
问题 I want to rotate a P3 (that is somewhere near) around a vector (that intersects P1 and P2) by x degrees. P1 and P2 are 2 points that are intersected by vector(line) e from image. I've researched and searched a lot and found some good materials, but my trigonometry skills are so poor. I need this for PAWN (small), here i have some code, but doesn't really work as intended. If anyone can help me, i'll be very thankful :) Image link: http://upload.wikimedia.org/wikipedia/commons/thumb/5/51/Euler