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 9213849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:50:40+00:00 2026-06-18T01:50:40+00:00

I have given a list of indices, e.g. i = [3 5] and a

  • 0

I have given a list of indices, e.g. i = [3 5] and a vector v = 1:6. I need a function f which returns the logical map for the vector v given the indices i, e.g.:

f(i, length(v)) = [0 0 1 0 1 0]

Since I will call this function several million times, I would like to make it as fast as possible. Is there a builtin function which performs this task?

  • 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-18T01:50:41+00:00Added an answer on June 18, 2026 at 1:50 am

    I know I’m late in the game, but I really wanted to find a faster solution which is just as elegant as ismember. And indeed there is one, that employs the undocumented ismembc function:

    ismembc(v, i)
    

    Benchmark

    N = 7;
    i = [3 5];
    
    %// slayton's solution
    tic
    for ii = 1:1e5
        clear idx;
        idx(N) = false;
        idx(i) = true;
    end
    toc
    
    %// H.Muster's solution
    tic
    for ii = 1:1e5
        v = 1:N;
        idx = ismember(v, i);
    end
    toc
    
    %// Jonas' solution
    tic
    for ii = 1:1e5
        idx = sparse(i, 1, true, N, 1);
    end
    toc
    
    %// ismembc solution
    tic
    for ii = 1:1e5
        v = 1:N;
        idx = ismembc(v, i);
    end
    toc
    

    Here’s what I got:

    Elapsed time is 1.482971 seconds.
    Elapsed time is 6.369626 seconds.
    Elapsed time is 2.039481 seconds.
    Elapsed time is 0.776234 seconds.
    

    Amazingly, ismembc is indeed the fastest!

    Edit:
    For very large values of N (i.e. when v is a large array), the faster solution is actually slayton’s (and HebeleHododo’s, for that matter). You have quite a variety of strategies to choose from, pick carefully 🙂

    Edit by H.Muster:
    Here’s are benchmark results including _ismemberoneoutput:

    Slayton's solution:
       Elapsed time is 1.075650 seconds.
    ismember:
       Elapsed time is 3.163412 seconds.
    ismembc:
       Elapsed time is 0.390953 seconds.
    _ismemberoneoutput:
       Elapsed time is 0.477098 seconds.
    

    Interestingly, Jonas’ solution does not run for me, as I get an Index exceeds matrix dimensions. error…

    Edit by hoogamaphone:
    It’s worth noting that ismembc requires both inputs to be numerical, sorted, non-sparse, non-NaN values, which is a detail that could be easily missed in the source documentation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write a function, that returns true if a given list is
I have a function that returns a comparison matrix from a given list: def
I have been given a staff list which is supposed to be up to
I have a UL list which has images in li. I have given UL
Hello I have a predicate that returns example equalisation for given list equalisation([1,2,3,4,12],L). L
Given that I have the following WCF service: class LookUpService { public List<County> GetCounties(string
I have a list of whitelisted words: kitchen chair table ; Given a text,
have a database with a list of business leads. They are all given a
I currently have a list of things to update in the given mysqli query:
I am given an input, N, i have to find the number of list

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.