vector

Count frequency of each element in vector

孤人 提交于 2021-01-29 03:27:31
问题 I'm looking for a way to count the frequency of each element in a vector. ex <- c(2,2,2,3,4,5) Desired outcome: [1] 3 3 3 1 1 1 Is there a simple command for this? 回答1: rep(table(ex), table(ex)) # 2 2 2 3 4 5 # 3 3 3 1 1 1 If you don't want the labels you can wrap in as.vector() as.vector(rep(table(ex), table(ex))) # [1] 3 3 3 1 1 1 I'll add (because it seems related somehow) that if you only wanted consecutive values, you could use rle instead of table : ex2 = c(2, 2, 2, 3, 4, 2, 2, 3, 4, 4)

Superimposing vectors, dgrid3d and pm3d in gnuplot for 3D plot

ε祈祈猫儿з 提交于 2021-01-29 02:57:49
问题 I am trying to plot a vector field in Gnuplot superimposing dgrid3d, pm3d and vectors. I can get dgrid3d and pm3d to work but when I try to superimpose the vectors weird things happen. I am trying the following: set dgrid3d 50,50,2 set pm3d at b set hidden3d splot 'v-field.dat' u 1:2:6 w l, "" u 1:2:3:4:5:6 w vectors Data is in the format: 0.0000000020000000 0.0833272880000000 0 0 0 1.62609277247135e-09 0.5000000020000001 0.5833272880000000 0 0 0 0.965930741599645 0.8749688835000000 0

Angles Between Continuous 2D Vectors

只谈情不闲聊 提交于 2021-01-28 23:01:24
问题 I'm having some trouble calculating the clockwise angles between continuous 2D vectors. My computed angles do not seem right when I compare them by eye on a plot. What follows is my process in R. If necessary, install and enable the "circular" package: install.packages('circular') library(circular) Generate a small data frame of 2D coordinates: functest <- data.frame(x=c(2,8,4,9,10,7),y=c(6,8,2,5,1,4)) Plot the points for reference: windows(height=8,width=8) par(pty="s") plot(functest, main =

C++ vector of priority_queue of strings with custom strings comparator

两盒软妹~` 提交于 2021-01-28 21:18:23
问题 I have a vector of priority_queue s of string s: vector<priority_queue<string>>> queues(VECTOR_CAPACITY); And the question is how can I apply custom string comparator to those priority_queue s? Should this be relevant, the comparator I want puts shorter strings before longer ones and when they're equal length uses standard strings comparison. I tried this way: auto comparator = [] (string s1, string s2) { return s1.length() < s2.length() || s1 < s2; }; vector<priority_queue<string, vector

Modifying one object in list modifies all objects in list

旧时模样 提交于 2021-01-28 13:52:49
问题 I am trying to make a list of coordinates directly adjacent to any given point in a 3d grid. For example, when given a vector {3,3,3}, the function should return the following list: [{4,3,3},{2,3,3},{3,4,3},{3,2,3},{3,3,4},{3,3,2}] (The values in curly braces are vector objects, not lists.) Here is my code: def touchingBlocks(sourceBlock): touching = [] for t in range(6): touching.append(sourceBlock) touching[0].x += 1 touching[1].x -= 1 touching[2].y += 1 touching[3].y -= 1 touching[4].z +=

Creating a Random Feature Array in Spark DataFrames

自闭症网瘾萝莉.ら 提交于 2021-01-28 12:15:56
问题 When creating an ALS model, we can extract a userFactors DataFrame and an itemFactors DataFrame. These DataFrames contain a column with an Array. I would like to generate some random data and union it to the userFactors DataFrame. Here is my code: val df1: DataFrame = Seq((123, 456, 4.0), (123, 789, 5.0), (234, 456, 4.5), (234, 789, 1.0)).toDF("user", "item", "rating") val model1 = (new ALS() .setImplicitPrefs(true) .fit(df1)) val iF = model1.itemFactors val uF = model1.userFactors I then

Element wise cross product of vectors contained in 2 arrays with Python

荒凉一梦 提交于 2021-01-28 08:15:55
问题 I have two arrays, one containing a list of vectors ( A ) and one containing a 2D list of vectors ( B ). I am looking to do an element wise cross product of the vectors in each array in a specific way. The fist vector in A should be cross producted (?) by all 3 vectors contained in the first element of B . Here is a minimal example: import numpy as np A = np.random.rand(2,3) B = np.random.rand(2,3,3) C = np.random.rand(2,3,3) C[0,0] = np.cross(A[0],B[0,0]) C[0,1] = np.cross(A[0],B[0,1]) C[0,2

How to create Map from Vector

流过昼夜 提交于 2021-01-28 07:53:23
问题 The containers package already contains functions to convert Set and List to Map but it's missing one for Vector from vector package. Is this because package developers do not want to have dependencies on other (similar) packages? Shouldn't be some general package which converts between data structures? In this way I always have to convert to List to convert to other structures. But I want to minimize use because of performance. 来源: https://stackoverflow.com/questions/41960585/how-to-create

Convert String to numeric vector

眉间皱痕 提交于 2021-01-28 05:35:21
问题 I have a function which takes a vector of zeros and ones for example: foo(c(1,1,1,0,1,1)) But in some cases, there are more then 100 numbers and I have to add all commas manually. Is there any predefined function to convert a string to such a vector? Something like this: foo(unknown_function("111011")) 回答1: We can use paste foo <- function(vec){ paste(vec, collapse="") } foo(c(1,1,1,0,1,1)) #[1] "111011" If we need to do the reverse foo1 <- function(str1){ as.integer(unlist(strsplit(str1, "")

Idris: proof about concatenation of vectors

对着背影说爱祢 提交于 2021-01-28 05:05:47
问题 Assume I have the following idris source code: module Source import Data.Vect --in order to avoid compiler confusion between Prelude.List.(++), Prelude.String.(++) and Data.Vect.(++) infixl 0 +++ (+++) : Vect n a -> Vect m a -> Vect (n+m) a v +++ w = v ++ w --NB: further down in the question I'll assume this definition isn't needed because the compiler -- will have enough context to disambiguate between these and figure out that Data.Vect.(++) -- is the "correct" one to use. lemma : reverse