How to identify what are the exact elements of a vector?

ismember matlab
matlab find position of value in array
how to extract elements from a vector in matlab
find element of array matlab
matlab find value in array
matlab find index of max value
matlab logical indexing
vector indexing

I have a vector x of the form:

x=c(601, 602, 603, 604, 605, 606, 607, 608, 609, 610, 611, 612, 613, 614,
 615, 616, 617, 618, 619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 630,
 631, 632, 633, 634, 635, 636, 637, 638, 639, 640, 641, 642, 643, 644, 645, 646,
 647, 648, 649, 650)

If length (x) == 50: I would like to test (TRUE or FALSE) if x is exactly equal to one of the intervals 1:50 or 51:100 or 101:150 or 151:200 .... or 951:1000.

Or if length (x)> 50, I would like to test (TRUE or FALSE) if x is exactly equal to the union of intervals composed by ((1:50 U 51: 100) or (1:50 U 101: 150) or (51 : 100 U 151: 200) ....).

My attempt:

    x == c(1:50) | 
    x == c(51:100) | 
    x == c(101:150) | 
    x == c(151:200) | 
    x == c(201:250) | 
    x == c(251:300) | 
    x == c(301:350) | 
    x == c(351:400) | 
    x == c(401:450) | 
    x == c(451:500) | 
    x == c(501:550) | 
    x == c(551:600) | 
    x == c(601:650) | 
    x == c(651:700) | 
    x == c(701:750) | 
    x == c(751:800) | 
    x == c(801:850) | 
    x == c(851:900) | 
    x == c(901:950) | 
    x == c(951:1000)

I would like to optimize this code.

PS: I'm not trying to have a frequency table of the x elements and intervals like this question. I want to know if x corresponds exactly to one or the union of those intervals.

a data.table solution:

test <- function(u){
                               by = cut(u, 
                                        breaks = seq(0, 1000, 50))][, unique(N)] == 50), 
x <- 1:50 # TRUE
y <- 2:51 # FALSE
z <- 1:100 # TRUE
w <- 2:101 # FALSE





Quantitative Methods for Finance and Investments, In a financial sense, where elements within a vector represent payoffs of a given For example, how does one determine the exact element to place in the third  If we want to identify all elements of our vectors, which are existent in each of the vectors, we can use a combination of the Reduce (), intersect (), and list () functions. Have a look at the following R code: Reduce ( intersect, list ( x1, x2, x3)) # Identify common elements # "A" "D".

You can use cut, i.e.

unique(cut(x, breaks = seq(0, 1000, by = 50)))
#[1] (600,650]

If you want a boolean If x is included in one those intervals, then you can do,

unique(cut(x, breaks = seq(0, 1000, by = 50))) != ''
#[1] TRUE

#or If you only want to be in 1 group, then as suggested by Ronak,
length(unique(cut(x, breaks = seq(0, 1000, by = 50)))) == 1
#[1] TRUE

Structural Analysis: A Unified Classical and Matrix Approach, Exact element matrices can be generated only for bars. Assemblage of stiffness matrices and load vectors of individual elements, Problems 17.1 Determine any element S* for the bar of Example 17.5, using the shape functions in Fig. 17.4. The typical element of \({\bf A}\) is \(a_{ij}\), denoting the element of row \(i\) and column \(j\). Matrix addition and subtraction Matrices are added and subtracted on an element-by-element basis.

Another option:

test <- function(x) length(x)%%50==0 & x[length(x)]%%50==0 & all(diff(x)==1)

length(x)%%50==0 ensures that the vector is of length that are multiples of 50.

x[length(x)]%%50==0 checks that the last element is divisible by 50.

all(diff(x)==1) checks that vector is a sequence in steps of 1. (e.g. x <- rep(50,50) will fail this part)


> test(1:50)
#[1] TRUE
> test(2:51)
#[1] FALSE
> test(1:100)
#[1] TRUE
> test(2:101)
#[1] FALSE

How Do You Define a Vector?, Vector: a quantity with more than one element (more than one piece of information). If you wanted, you could find the magnitude of this vector displacement with a value of 3.61 meters. The Zero Vector in Actual Physics. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Finding All Positions for Multiple Elements in a Vector

Find indices and values of nonzero elements, To find a specific integer value, use the == operator. For instance, find the element equal to 13 in a 1-by-10 vector of odd integers. x = 1:2:20. x = 1×10 1 3 5 7 9  Selecting Vector Elements x[4] The fourth element. x[-4] All but the fourth. x[2:4] Elements two to four. x[-(2:4)] All elements except two to four. x[c(1, 5)] Elements one and five. x[x == 10] Elements which are equal to 10. x[x < 0] All elements less than zero. x[x %in% c(1, 2, 5)] Elements in the set 1, 2, 5. By Position By Value Named

Find Array Elements That Meet a Condition, This example shows how to filter the elements of an array by applying conditions to of the array elements that meet a condition rather than their actual values. MATLAB ® treats the array as a single column vector with each column appended to the bottom of the previous column. Thus, linear indexing numbers the elements in the columns from top to bottom, left to right. For example, consider a 3-by-3 matrix. You can reference the A(2,2) element with A(5), and the A(2,3) element with A(8).

Advances in Information Retrieval: 29th European Conference on IR , The two less similar vectors of A define the area(a1,a2), which is the So we can decide whether there is an exact name in two Finding the best lower name means finding a query q that ranks first the biggest number of elements of A, i.e.  A colleague came to my office the other day with an interesting question: Is there a way in R to find the closest number to X in a list? I knowing full well the power the power of R, I naturally said that surely there is such a function, but I have never used it.

  • you need to library(data.table) before defining the function
  • @RonakShah right...I thought they wanted which interval...I ll edit
  • This code does not work if x = c (1: 103), it gives TRUE whereas (1: 103) is not exactly equal to one of the intervals 1:50 or 51: 100 or 101: 150 nor to the union (1:50) u (51: 100)....