Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which(vector1 < vector2)

Let's make a small example first, that computes in R:

x<- c(1,3,1,4,2) max(which(x<2)) [1] 3 

Now, I would like to do this not just for one value 2, but for many values simultaneously. It should give me something like that:

max(which(x<c(1,2,3,4,5,6))) [1] NA 3 5 5 5 5 

Of course I could run a for loop, but that is very slow:

for(i in c(1,2,3,4,5,6)){     test[i]<-max(which(x<i)) } 

Is there a fast way to do this?

like image 515
MrHallo Avatar asked May 11 '15 14:05

MrHallo


People also ask

What is Vector3 and Vector2?

The magnitude of a Vector2 equals sqrt(x^2+y^2) . A Vector3 has a 3D direction, like a xyz point in a 3D space, or a color in RGB format, or a set of three numbers. e.g. (0,0,0) or (-0.1, 3.14, 30). The magnitude of a Vector3 equals sqrt(x^2+y^2+z^2) .

What is Vector2 in C#?

Min(Vector2, Vector2) Returns a vector whose elements are the minimum of each of the pairs of elements in two specified vectors. Multiply(Single, Vector2) Multiplies a scalar value by a specified vector. Multiply(Vector2, Single)

What is a Vector2 variable?

Vector2 is a type of variable, like int, float, bool, string ... Position is a value of a gameobject (Transform component of an object contains the position value, and rotation and scale) Position in a 2D world, have 2 coordinates, so a VEctor2 variable is used to define the position.

What is a vector 3 Unity?

Vector 3 is a struct https://docs.unity3d.com/ScriptReference/Vector3.html and is commonly used to reference the X, Y, and Z position of an object. It can also be used for detecting direction and also used with rotations as well.


1 Answers

Try this:

vapply(1:6, function(i) max(which(x < i)), double(1)) 
like image 87
David Arenburg Avatar answered Oct 04 '22 07:10

David Arenburg