Tags down


Generating a pairwise 'distance' matrix

By : Mohan Phatak
Date : October 18 2020, 08:10 PM
it helps some times This is a cumulative distance, so take a cumulative sum and then do the distance calculation:
code :
mat <- c(3,10,7,9)
#   1  2  3  4
#2  3         
#3 13 10      
#4 20 17  7   
#5 29 26 16  9

Share : facebook icon twitter icon

how to transfer the pairwise distance value in a distance matrix

By : Immanuel Voki Amoako
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Based on the comments, you know how to get the data into the form of an N x 3 matrix, called X, where X(:,1) is the first index, X(:,2) is the second index, and X(:,3) is the corresponding distance.
Let's assume that the indices (id1... idx) are arbitrary numeric labels.
code :
% First, build a list of all the unique indices    
indx = unique([X(:,1); X(:,2)]);
Nindx = length(indx);

% Second, initialize an empty connection matrix, C
C = zeros(Nindx, Nindx);  %or you could use NaN(Nindx, Nindx)

% Third, loop over the rows of X, and map them to points in the matrix C
for n = 1:size(X,1)
     row = find(X(n,1) == indx);
     col = find(X(n,2) == indx);
     C(row,col) = X(n,3);

Pairwise distance two matrix R

By : anbang
Date : March 29 2020, 07:55 AM
like below fixes the issue You can use the proxy package for these problems. By default, proxy::dist considers each row of a matrix or data frame as a single "object".
code :

proxy::dist(mat1, mat2, method="Manhattan")
     [,1]  [,2]  [,3] 
[1,]  4804  4832  4656
[2,]  3708  3736  3560
[3,] 17407 17435 17259

proxy::dist(mat1, mat2, method="Euclidean")
     [,1]      [,2]      [,3]     
[1,]  3397.036  3417.059  3295.962
[2,]  2761.996  2787.495  2708.075
[3,] 12308.674 12328.422 12204.286

numpy python: vectorize distance function to calculate pairwise distance of 2 matrix with a dimension of (m, 3)

By : altalapita
Date : March 29 2020, 07:55 AM
Does that help You can use np.newaxis to expand the dimensions of your two arrays A and B to enable broadcasting and then do your calculations.
Pairwise distance means every point in A (m, 3) should be compared to every point in B (n, 3). This results in a (m, n) matrix of distances. With numpy one can use broadcasting to achieve the wanted result. By using A=A[:, np.newaxis, :] and B=B[np.newaxis, :, :] the resulting shapes are A (m, 1, 3) and B(1, n, 3) respectivley. If you then perform a calculation like C = A-B numpy automatically broadcasts. This means you get a copy of all m rows of A for all n columns of B and a copy of all n columns of B for all m rows of A.
code :
  A (m, 1, 3)
- B (1, n, 3)
= C (m, n, 3)
import numpy as np
m = 10
n = 12
A = np.random.random((m, 3))
B = np.random.random((n, 3))

# Add newaxis on seconbd axis of A and on first axis on B
# shape: (m, n, 3) = (m, 1, 3) - (1, n, 3)
C = A[:, np.newaxis, :] - B[np.newaxis, :, :]

C = np.linalg.norm(C, axis=-1)
# shape: (m, n)

Fast Algorithms for Finding Pairwise Euclidean Distance (Distance Matrix)

By : thetouccher
Date : March 29 2020, 07:55 AM
Hope this helps Well, I couldn't resist playing around. I created a Matlab mex C file called pdistc that implements pairwise Euclidean distance for single and double precision. On my machine using Matlab R2012b and R2015a it's 20–25% faster than pdist(and the underlying pdistmex helper function) for large inputs (e.g., 60,000-by-300).
As has been pointed out, this problem is fundamentally bounded by memory and you're asking for a lot of it. My mex C code uses minimal memory beyond that needed for the output. In comparing its memory usage to that of pdist, it looks like the two are virtually the same. In other words, pdist is not using lots of extra memory. Your memory problem is likely in the memory used up before calling pdist (can you use clear to remove any large arrays?) or simply because you're trying to solve a big computational problem on tiny hardware.

Distance matrix to pairwise distance list in R

By : Taklit
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Is there any R package to obtain a pairwise distance list if my input file is a distance matrix For eg, if my input is a data.frame like this: , If you have a data.frame you could do something like:
Related Posts Related Posts :
  • Multinomial probit regression with mixed type explanatory variables
  • How can I make a variable in a dataset containing a vector of all numbers between two other variables?
  • How to extract the trailing digits from a string in R?
  • Select values based on other columns
  • readLines killing R in purrr::map
  • Subset rows based on "start and stop" strings
  • How to add a column to lists within a list without losing their names?
  • Plotting the means in ggplot, without using stat_summary()
  • R :Looping through each 5 rows of data frame and imputing incremental value
  • In R, is growing a list just as inefficient as growing a vector?
  • Flexdashboard, rhandsontable: how to programmatically access user updated table?
  • Creating Summary Table from R Variables
  • Average over groups and include previous groups
  • R: data.table count rows on specific columns > 0
  • Transform (shuffle) just 2 Fields in a Dataframe
  • Issue with replacing string by match in R
  • (very) Simple quantstrat trading model using logistic regression
  • R - count maximum number of consecutive dates
  • Problems using tidyr separate on "|"
  • Default value when calling a function in a for loop
  • Finding values in a matrix from list of values in R
  • count 0's in a zoo (or dataframe) object
  • Finding the first non-zero year in data frame for multiple variables using tidyverse
  • ggplot2 - how to assign geom_text with arrow icon to second yaxis scale
  • regex fails with dollar sign
  • Drop first element of list of lists, condense list of lists? Too many elements?
  • R - how to apply output of ifelse(str_detect ...) to whole group
  • caret package confusion matrix define positive case with multiple classes
  • Change all R columns names using a reference file
  • In R & dabestr, how do I get grouped differences correctly?
  • Exclude or set a unique color to the bottom triangle of a correlation matrix heatmap
  • r shiny observe function clears text input
  • Split column by multiple delimiters, keeping delimiters
  • How to random search in a specified grid in caret package?
  • merge 2 data frames in a loop for each column in one of them
  • how to edit the codes for the summary of R S4 Object?
  • Remove specific rows in R
  • Flatten JSON list into data frame
  • Filtering a dataset and making a ggplot
  • Align cells vertically to be at the bottom flextable
  • R speed up sapply
  • invalid subscript type 'list' Azure Machine Learning
  • Use rollapply with xts object and an anonymous defined function
  • Isolate data frames from a spreadsheet to create a list
  • Error in xts, as.POSIXct "'order.by' cannot contain 'NA', 'NaN', or 'Inf'"
  • Column splitting in R
  • number similar/duplicated rows in R
  • Count the number of times each value appears in a row dataframe r
  • how to vectorise my code in r using for loop?
  • A function to fill in a column with NA of the same type
  • Network flow balancing constraint in R
  • Adding main titles from list to graphs in for loop
  • create a matrix in Perl or R if data is provided in CSV file
  • Passing column names as string to with
  • R - filtering rows and summing
  • How to change the order of fill aesthetic in faceted ggplot?
  • Function to remove outliers by group from dataframe
  • How to find the difference of max & min values in one group in a variable in a dataframe
  • Convert unicode to a readable string
  • Wrong scale/difficult to interpret times on time series object using 'ts'
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org