matrix

Distance matrix in R

拟墨画扇 提交于 2020-01-05 01:25:05
问题 I have to create a distance matrix using R. My data is in an Excel file which contains 300 rows and 10 columns. I have to create distance matrix based on the values of 9th column. For example s s s s s s 1 s 2 2 s 3 3 4 s 4 4 7 3 s 5 5 8 2 8 How to create this type of matrix? 回答1: Easiest option I know, is to save your Excel sheet containing the data as a CSV file. Make sure that only the first row and column of the sheet contain any sample or variable names. Then read into R using: dat <-

2d array, all values are adjacent

旧时模样 提交于 2020-01-04 23:21:24
问题 I need a fast way to check if a 2d array is entirely adjacent, meaning all values are adjacent to other same values. Adjacent means the four cardinal directions. This would be an adjacent array [1 1 2] [1 2 2] [3 3 3] This isn't [1 2 1] [1 2 2] [3 3 3] This is [1 2 4] [1 2 2] [3 3 3] So far, I've tried an O(M * N) method where I go through the whole array and check if at least one of the neighbors is the same value. I'm looking for a possibly faster way. EDIT: just noticed my method doesn't

Pointers to a 2d array and manual memory management - C

左心房为你撑大大i 提交于 2020-01-04 21:40:16
问题 I decided it would be a nice challenge to build a library to handle all kinds of matrix calculations, in pure C. Now, even though I have some pretty good experience with Objective-C and Cocoa, my knowledge of C is just what I need to work with Objective-C and not much more. So, for example, I'm familiar with the concept of pointer, arrays, etc in C, but not with malloc and free (ARC is a bliss!). I decided to embrace this project so I can get more C experience (besides having loads of fun

Pointers to a 2d array and manual memory management - C

梦想的初衷 提交于 2020-01-04 21:40:09
问题 I decided it would be a nice challenge to build a library to handle all kinds of matrix calculations, in pure C. Now, even though I have some pretty good experience with Objective-C and Cocoa, my knowledge of C is just what I need to work with Objective-C and not much more. So, for example, I'm familiar with the concept of pointer, arrays, etc in C, but not with malloc and free (ARC is a bliss!). I decided to embrace this project so I can get more C experience (besides having loads of fun

Can R do operations like cumsum in-place?

瘦欲@ 提交于 2020-01-04 16:57:23
问题 In Python I can do this: a = np.arange(100) print id(a) # shows some number a[:] = np.cumsum(a) print(id(a)) # shows the same number What I did here was to replace the contents of a with its cumsum. The address before and after is the same. Now let's try it in R: install.packages('pryr') library(pryr) a = 0:99 print(address(a)) # shows some number a[1:length(a)] = cumsum(a) print(address(a)) # shows a different number! The question is how can I overwrite already-allocated memory in R with the

correlation matrix MATLAB

房东的猫 提交于 2020-01-04 14:29:13
问题 Do you know a matlab script that gets the correlation matrix of a set of data like numbe of columns=7, and rows = n>>7 x1 x2 x3 x4 x5 x6 x7 0.780 2.042 2.754 2.894 5.000 5.000 5.000 0.767 0.816 2.758 2.898 5.000 1.644 1.626 0.816 0.836 1.558 2.124 2.952 5.000 5.000 0.848 0.871 1.588 2.147 2.966 5.000 5.000 1.378 1.620 1.433 2.567 3.268 3.203 5.000 .... 回答1: Try corrcoef. In general, you can find these sort of things immediately by searching the online help, for example via the convenient

Java optimized Cramers rule function

流过昼夜 提交于 2020-01-04 14:22:11
问题 Recently learned about Cramers rule in precalculus, and decided to make an algorithm in Java to help me understand it better. The following code works 100% correctly, however it does not use any sort of for loop to do what it does in a much simpler fashion. Question: Is there a more elegant implementation of Cramers Rule in Java? I'm thinking that making a basic determinant method, and then doing some column swapping for when I need to take the determinant of Dx, Dy, and Dz. (for Dx, swap

(DirectX) Generating a rotation matrix to match a vector

纵然是瞬间 提交于 2020-01-04 14:03:28
问题 How can I take a vector and generate a matrix that will rotate a mesh to face along that vector? Specifics: I want the model for my player to rotate to face the direction that the camera is looking. Right now, it always points in the direction it started in, and turning just makes the camera look at the player's side. I have experimented with D3DXMatrixYawPitchRoll, with partial success. However, I noticed some drift in time from perfect alignment, and it had some strange wobbly issues when I

Inner matrix dimensions must agree

两盒软妹~` 提交于 2020-01-04 10:11:34
问题 I have a matrix A and a vector x : A is a 50x30 matrix x is a 1x30 vector I want to multiply A by x , yet whenever I try z = A * x I get the error Inner matrix dimensions must agree. Yet surely with the same amount of columns the matrix dimensions do agree? I'm confused as to why this works: A = rand(2,2); x = [1;2]; A * x Yet this does not work: A = rand(2,2); x = 1:2; A * x 回答1: Transpose the second argument: z = A * x.' As the error suggest - the inner matrix dimensions must agree - you

Automatic differentiation (AD) with respect to list of matrices in Haskell

我只是一个虾纸丫 提交于 2020-01-04 10:06:22
问题 I am trying to understand how can I use Numeric.AD (automatic differentiation) in Haskell. I defined a simple matrix type and a scalar function taking an array and two matrices as arguments. I want to use AD to get the gradient of the scoring function with respect to both matrices, but I'm running into compilation problems. Here is the code {-# LANGUAGE DeriveTraversable, DeriveFunctor, DeriveFoldable #-} import Numeric.AD.Mode.Reverse as R import Data.Traversable as T import Data.Foldable as