logo
down
shadow

sorting vectors based on size()


sorting vectors based on size()

By : pwelo
Date : October 16 2020, 11:12 AM
it fixes the issue i have a 2-d vector like vector < vector < coordinates > > v( points); where coordinates class is: , 1) make a function that compares two vectors based on size:
code :


Share : facebook icon twitter icon
Sorting vectors based on length in C

Sorting vectors based on length in C


By : Strong Lei
Date : March 29 2020, 07:55 AM
will be helpful for those in need I want to make a programm that takes the dimension and numbers of vectors to be sorted based on their length. Most of the code works but the sort part of the program doesnt. Basically what I want to do there is: compare the output from the bereken_lengte function from 2 places in the array w. But nothing seems to happen. Also in the function bereken_lengte, I cant take the roots of the sum after the loop ended.
code :
double bereken_lengte(double *array, int dim)
{
  unsigned int i;
  double sum =0.0;
  for(i=0; i<dim; ++i)
    sum += pow(array[i],2);
  return sum;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double bereken_lengte(double *array, size_t dim)
{
  size_t i;
  double sum=0.0;
  for(i=0; i<dim; ++i)
    sum += pow(array[i],2);
  return sum;
}

        /* this is ugly: qsort only allows only two arguments */
static size_t ze_third_argument=0;
int srt_pdbl(void *l, void *r)
{
double **dl = l, **dr = r;
double diff;
diff = bereken_lengte( *dl, ze_third_argument) - bereken_lengte( *dr, ze_third_argument) ;
return (int) diff;
}

void sort_vector(double *w[] , size_t num , size_t dik )
{
ze_third_argument = dik;
qsort(w, num, sizeof *w, srt_pdbl );
}

int main (void)
{
  size_t dim, num;
  size_t i, j,k,l;
  double **w;
  scanf ("%zu %zu", &dim, &num);          /* read N and M */
  w = calloc (num, sizeof *w);  /* allocate array of M pointers */
  for (i = 0; i < num; i++)
  {
    /* allocate space for N dimensional vector */
    w[i] = calloc (dim, sizeof *w[i] );
    /* read the vector */
    for (j = 0; j < dim; j++)
    {
      scanf ("%lf", &w[i][j]);
    }
  }
  sort_vector(w,num,dim);
  for(k=0; k<num; ++k)
  {
    printf("\n");
    for(l=0; l<dim; ++l)
      printf("%f ", w[k][l]);
  }
  return 0;
}
Sorting Vectors based on their correlation

Sorting Vectors based on their correlation


By : William John-Pierre
Date : March 29 2020, 07:55 AM
will help you Pearsons will work. So will the usual dot product, Manhattan, etc.
You realize, of course, that this needs a matrix.
Splicing two different length vectors based on their respective index vectors containing global addresses to new vectors

Splicing two different length vectors based on their respective index vectors containing global addresses to new vectors


By : user2238658
Date : March 29 2020, 07:55 AM
will be helpful for those in need One approach that occurs to me is to do a parallel binary search, taking each value from the global index vector, and seeing if it has a match in the key vector. If it has a match in the key vector, then place the corresponding value in the result. If it does not have a match in the key vector, then place 0 in the result.
So for each position in:
code :
ind[]={0,1,2,3,4,6,8,10,12}
indy[]={1, 2, 3, 4};
#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/binary_search.h>
#include <thrust/copy.h>

#define MAX_DSIZE 32768
#define nTPB 256


struct extend_functor
{
  int vec_len;
  int *vec1;
  float *vec2;
  extend_functor(int *_vec1, int _vec_len, float *_vec2) : vec1(_vec1), vec2(_vec2),  vec_len(_vec_len) {};
  template <typename Tuple>
  __device__ __host__
  void operator()(const Tuple &my_tuple) {
    float result = 0.0f;
    if (thrust::get<1>(my_tuple) < vec_len)
      if (thrust::get<0>(my_tuple) == vec1[thrust::get<1>(my_tuple)]) result = vec2[thrust::get<1>(my_tuple)];
    thrust::get<2>(my_tuple) = result;
  }

};

// binary search, looking for key in a

__device__ void bsearch_range(const int *a, const int key, const unsigned len_a, unsigned *idx){
  unsigned lower = 0;
  unsigned upper = len_a;
  unsigned midpt;
  while (lower < upper){
    midpt = (lower + upper)>>1;
    if (a[midpt] < key) lower = midpt +1;
    else upper = midpt;
    }
  *idx = lower;
  return;
  }


// k is the key vector
// g is the global index vector
// v is the value vector
// r is the result vector

__global__ void extend_kernel(const int *k, const unsigned len_k, const int *g,  const unsigned len_g, const float *v, float *r){
  unsigned idx = (blockDim.x * blockIdx.x) + threadIdx.x;
  if (idx < len_g) {
    unsigned my_idx;
    int g_key = g[idx];
    bsearch_range(k, g_key, len_k, &my_idx);
    int my_key = -1;
    if (my_idx < len_k)
      my_key = k[my_idx];
    float my_val;
    if (g_key == my_key) my_val = v[my_idx];
    else my_val = 0.0f;
    r[idx] = my_val;
    }
}


int main(){

  int len_x = 7;
  int len_y = 4;
  int len_g = 9;
  int indx[]={0,2,4,6,8,10,12};
  int indy[]={1, 2, 3, 4};
  float dnx[]={91.0f,92.0f,93.0f,94.0f,95.0f,96.0f,97.0f};
  float dny[]={61.0f,62.0f,63.0f,64.0f};
  int ind[]={0,1,2,3,4,6,8,10,12};

  int *h_k, *d_k, *h_g, *d_g;
  float *h_v, *d_v, *h_r, *d_r;

  h_k = (int *)malloc(MAX_DSIZE*sizeof(int));
  h_g = (int *)malloc(MAX_DSIZE*sizeof(int));
  h_v = (float *)malloc(MAX_DSIZE*sizeof(float));
  h_r = (float *)malloc(MAX_DSIZE*sizeof(float));

  cudaMalloc(&d_k, MAX_DSIZE*sizeof(int));
  cudaMalloc(&d_g, MAX_DSIZE*sizeof(int));
  cudaMalloc(&d_v, MAX_DSIZE*sizeof(float));
  cudaMalloc(&d_r, MAX_DSIZE*sizeof(float));

// test case x

  memcpy(h_k, indx, len_x*sizeof(int));
  memcpy(h_g, ind, len_g*sizeof(int));
  memcpy(h_v, dnx, len_x*sizeof(float));

  cudaMemcpy(d_k, h_k, len_x*sizeof(int), cudaMemcpyHostToDevice);
  cudaMemcpy(d_g, h_g, len_g*sizeof(int), cudaMemcpyHostToDevice);
  cudaMemcpy(d_v, h_v, len_x*sizeof(float), cudaMemcpyHostToDevice);
  extend_kernel<<<(len_g+nTPB-1)/nTPB, nTPB >>>(d_k, len_x, d_g, len_g, d_v, d_r);
  cudaMemcpy(h_r, d_r, len_g*sizeof(float), cudaMemcpyDeviceToHost);

  std::cout << "case x result: ";
  for (int i=0; i < len_g; i++)
    std::cout << h_r[i] << " ";
  std::cout << std::endl;


// test case y

  memcpy(h_k, indy, len_y*sizeof(int));
  memcpy(h_g, ind, len_g*sizeof(int));
  memcpy(h_v, dny, len_y*sizeof(float));

  cudaMemcpy(d_k, h_k, len_y*sizeof(int), cudaMemcpyHostToDevice);
  cudaMemcpy(d_g, h_g, len_g*sizeof(int), cudaMemcpyHostToDevice);
  cudaMemcpy(d_v, h_v, len_y*sizeof(float), cudaMemcpyHostToDevice);
  extend_kernel<<<(len_g+nTPB-1)/nTPB, nTPB >>>(d_k, len_y, d_g, len_g, d_v, d_r);
  cudaMemcpy(h_r, d_r, len_g*sizeof(float), cudaMemcpyDeviceToHost);

  std::cout << "case y result: ";
  for (int i=0; i < len_g; i++)
    std::cout << h_r[i] << " ";
  std::cout << std::endl;

// using thrust

  thrust::device_vector<int> tind(ind, ind+len_g);
  thrust::device_vector<int> tindx(indx, indx+len_x);
  thrust::device_vector<float> tdnx(dnx, dnx+len_x);
  thrust::device_vector<float> tresult(len_g);

  thrust::device_vector<int> tbound(len_g);
  thrust::lower_bound(tindx.begin(), tindx.end(), tind.begin(), tind.end(), tbound.begin());
  thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(tind.begin(), tbound.begin(), tresult.begin())), thrust::make_zip_iterator(thrust::make_tuple(tind.end(), tbound.end(), tresult.end())), extend_functor(thrust::raw_pointer_cast(tindx.data()), len_x, thrust::raw_pointer_cast(tdnx.data())));

  std::cout << "thrust case x result: ";
  thrust::copy(tresult.begin(), tresult.end(), std::ostream_iterator<float>(std::cout, " "));
  std::cout << std::endl;
  return 0;
}
Sorting two column vectors into 3D matrix based on position

Sorting two column vectors into 3D matrix based on position


By : user3382685
Date : March 29 2020, 07:55 AM
Any of those help I've developed a solution, which works for my case but might not be as robust as required for some. It requires a known number of dots in a 'square' grid and a rough idea of the spacing between the dots. I find the 'AlphaShape' of the dots and all the points that lie along the edge. The edge vector is shifted to start at the minimum and then wrapped around a matrix with the corresponding points are discarded from the list of vertices. Probably not the best idea for large point clouds but good enough for me.
code :
R = 50; % search radius
xy = centres2;
x = centres2(:,1);
y = centres2(:,2);

for i = 1:8
    T = delaunay(xy); % delaunay
    [~,r] = circumcenter(triangulation(T,x,y)); % circumcenters
    T = T(r < R,:); % points within radius
    B = freeBoundary(triangulation(T,x,y)); % find edge vertices
    A = B(:,1);

    EdgeList = [x(A) y(A) x(A)+y(A)]; % find point closest to origin and rotate vector
    [~,I] = min(EdgeList);
    EdgeList = circshift(EdgeList,-I(3)+1);

    n = sqrt(length(xy)); % define zeros matrix
    matX = zeros(n); % wrap x vector around zeros matrix
    matX(1,1:n) = EdgeList(1:n,1);
    matX(2:n-1,n) = EdgeList(n+1:(2*n)-2,1);
    matX(n,n:-1:1) = EdgeList((2*n)-1:(3*n)-2,1);
    matX(n-1:-1:2,1) = EdgeList((3*n)-1:(4*n)-4,1);

    matY = zeros(n); % wrap y vector around zeros matrix
    matY(1,1:n) = EdgeList(1:n,2);
    matY(2:n-1,n) = EdgeList(n+1:(2*n)-2,2);
    matY(n,n:-1:1) = EdgeList((2*n)-1:(3*n)-2,2);
    matY(n-1:-1:2,1) = EdgeList((3*n)-1:(4*n)-4,2);

    centreMatX(i:n+i-1,i:n+i-1) = matX; % paste into main matrix
    centreMatY(i:n+i-1,i:n+i-1) = matY;

    xy(B(:,1),:) = 0; % discard values
    xy = xy(all(xy,2),:);   
    x = xy(:,1);
    y = xy(:,2);   

end

centreMatX(centreMatX == 0) = x;
centreMatY(centreMatY == 0) = y;
Sorting a list of unequal-size vectors in r

Sorting a list of unequal-size vectors in r


By : Sarah Small-Doom
Date : March 29 2020, 07:55 AM
may help you . Suppose I have several vectors - maybe they're stored in a list, but if there's a better data structure that's fine too:
code :
ll <- list(c(1,3,2),
           c(1,2),
           c(2,1),
           c(1,3,1))
sortfun <- function(l) {
  l1 <- lapply(l, function(x, n) {
    length(x) <- n
    x
  }, n = max(lengths(l)))
  l1 <- do.call(rbind.data.frame, l1)
  l[do.call(order, l1)] #order's default is na.last = TRUE
}

sortfun(ll)

#[[1]]
#[1] 1 2
#
#[[2]]
#[1] 1 3 1
#
#[[3]]
#[1] 1 3 2
#
#[[4]]
#[1] 2 1
Related Posts Related Posts :
  • C++ Struct Constructor With Arguments Separated Into Header and Implementation Files Fails When Single Monolithic File D
  • Object member variable updating in function but not persisting
  • How to find the magic number of a file by just provding the path of the file using c++?
  • How to export function names and variable names using GCC or clang?
  • What is the quickest way to print a 2D array of chars?
  • Should a call to WSAResetEvent after WSAEnumNetworkEvents cause event to never be set again?
  • File pointer only read the first entry
  • Using alias from header file in corresponding source file
  • Can I Initialize a char[] with a Ternary?
  • C++ Metaprogramming: Generating a byte sequence based on type/value input?
  • Scope of an object created inside if-condition?
  • Process std::map<K,V> and std::map<K,V*> in similar way
  • Windows Sensor API - Unresolved external symbol for CLSID
  • Matlab trigonometric functions
  • C++: Cannot compare double from an array of structures to another double in if function
  • Uniform initialization causes runtime error in C++
  • Sum of partial group of {^(0),(1),……,()}
  • Template in a Template of a different types problem
  • Undefined symbol boost::system:detail::system_category_instance
  • Access to a string member in C++
  • Template: How to select smallest int type which holds n bits?
  • Passing command line arguments to Visual Studio using CMake for profile guided optimization
  • Start multiple threads and wait only for one to finish to obtain results
  • Shall structured binding to a copy of a const c-array be const?
  • Meyers Singleton : static keyword confusion
  • every data type reserves a memory or address in array but why don't comma(,) reserve its memory in an array
  • c++ std::vector of functions passed as a parameter of a class method
  • Default parameters in constructor gives compile time errors
  • C++ set iterator error: no match for ‘operator+=’
  • Can I get the size of a dynamicly allocated array the way `delete[]` does?
  • properly rendering an SDL rectangle to the screen
  • STL not release memory from System level
  • Passing multidimensional array back through access members
  • About GDI/GDI+ coordinate compatibility?
  • What's the difference between these two object instantiation approaches?
  • How could a member method delete the object?
  • whats wrong with this program
  • Using .lib and .dll files in Linux
  • how to define a class using c++
  • C++ How to read in a text file and reverse the lines so it reads from bottom up
  • how do i make this so everything can use it? C++
  • Exporting a C++ class from a .SO ( linux )
  • intellisense for empty Win32 projects
  • Parsing mathematical functions of custom types
  • Android play raw Audio from C++ side
  • How to access Active Directory using C++Builder?
  • String statements
  • Instruction-Level-Parallelism Exploration
  • Select from SQLite with Qt
  • Dynamic Memory Allocation
  • Maximum number of characters in a string
  • Float Values as an index in an Array in C++
  • Is 'using namespace std;' a standard C++ function?
  • Building a suffix tree in C++
  • Passing ifstream to a function in C++
  • Where would you use a friend function vs. a static member function?
  • Invalid free while performing a std::string assign with -O2 set in g++
  • Shutting down multithreaded NSDocument
  • C(++) malloc confusion
  • Portable c++ atomic swap (Windows - GNU/Linux - MacOSX)
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org