Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8998277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:00:13+00:00 2026-06-16T00:00:13+00:00

I have a vector v . I would like to find the index of

  • 0

I have a vector v. I would like to find the index of the minimum difference of the elements of a vector v which is less that 1e-2. How can I get the index?

    v = c(0.0002873771, 0.0006478544, 0.0092186701, 0.0267084167, 
        0.0457307072 , 0.3176459806)
    D = abs(diff(v)) < 1e-2

I want to get that the index = 1 not 1 and 2, just the minimum one.
If we have this vector

    v = c( 0.01144003, 0.04644231, 0.05527114, 0.31680614)
    D = abs(diff(v)) < 1e-2
    index = which.min(abs(diff(v)) < 1e-2 )

It give me the index = = 1 but actually my desire index is 2 not 1.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:00:14+00:00Added an answer on June 16, 2026 at 12:00 am

    If you know that the minimum absolute difference is less than your tolerance, which.min is the correct function, and there is no need for D.

    > which.min(abs(diff(v)))
    [1] 1
    

    This code, in your edited question:

    index = which.min(abs(diff(v)) < 1e-2 )
    

    is taking the which.min of a logical vector. Not what you intend. This is what you (probably) want:

    v = c( 0.01144003, 0.04644231, 0.05527114, 0.31680614)
    > which.min(abs(diff(v)))
    [1] 2
    

    To actually check for your tolerance, you can use any and if. Here, I assume you want NA to be returned if all differences exceed your tolerance:

    min.difference.index <- if (any(abs(diff(v)) < 1e-2)) which.min(abs(diff(v))) else NA
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how can I find the index positions of elements
I have a vector of pairs representing hops between nodes which I would like
I have string and character vector. I would like to find all strings in
I have a sorted std::vector<int> and I would like to find the longest 'streak
I have two vectors, v1 and v2. I would like to find any of
What I would like to do (in Clojure): For example, I have a vector
I have a vector of pointers. I would like to call a function for
I have a vector of Rect: vector<Rect> myRecVec; I would like to remove the
I have a 3-vector, let's say v = vec3(-4, 2, 3) I would like
I have an std::vector<unsigned char> with binary data in. I just would like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.