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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:31:00+00:00 2026-06-12T07:31:00+00:00

I have a large alphabetically ordered cell array of strings (~495 thousand), with lots

  • 0

I have a large alphabetically ordered cell array of strings (~495 thousand), with lots of duplicates (which are next to each other, because it’s alphabetical).

For a given look-up string, I need to find all the strings in the list which will match the one I pass in.

I’ve been using strcmp(lookUpString,list) to do this, but this is extremely slow– I think it’s going through each value in the list to compare, because it doesn’t know it’s alphabetically sorted.

I could write a while loop to iterate through the list to compare each string using strcmp until I find the block of strings I want (and then stop), but I was wondering if there was a “matlab” way of doing this (i.e. performing logical comparison operations on a sorted array).

Thanks for your help!

  • 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-12T07:31:01+00:00Added an answer on June 12, 2026 at 7:31 am

    UPDATE: I was not satisfied with my earlier “Method 3” so I’ve just re-jigged it a little to get better performance. It now runs almost 10 times faster than a naive strcmp.

    strcmp wins on my machine (2011b on Linux Mint 12). In particular, it works much better than ismember. However, you can gain a bit of an extra speed up if you do some manual presorting yourself. Consider the following speed test:

    NumIter = 100;
    N = 495000;
    K = N / 20;
    List = cell(N, 1);
    for i = 1:20
        List(i*K - K + 1:i*K) = cellstr(char(i+96));
    end
    
    StrToFind = cell(NumIter, 1);
    for j = 1:NumIter
        StrToFind{j} = char(round(rand * 20) + 96);
    end
    
    %# METHOD 1 (ismember)
    tic
    for j = 1:NumIter
        Index1 = ismember(List, StrToFind{j});
        Soln1 = List(Index1);
    end
    toc
    
    %#METHOD 2 (strcmp)
    tic
    for j = 1:NumIter
        Index2 = strcmp(StrToFind{j}, List);
        Soln2 = List(Index2);
    end
    toc
    
    %#METHOD 3 (strmp WITH MANUAL PRE-SORTING)    
    tic
    for j = 1:NumIter
        CurStrToFind = StrToFind{j};
        K = 100;
        I1 = zeros(K, 2); I1(1, :) = ones(1, 2);
        I2 = zeros(K, 2); I2(end, 1) = 1; I2(end, 2) = N;
        KDiv = floor(N/K);
        for k = 2:K-1
            CurSearchNum = k * KDiv;
            CurListItem = List{CurSearchNum};
            if CurListItem < CurStrToFind; I1(k, 1) = 1; end;
            if CurListItem > CurStrToFind; I2(k, 1) = 1; end;
            I1(k, 2) = CurSearchNum; I2(k, 2) = CurSearchNum;
        end
        a = find(I1(:, 1), 1, 'last');
        b = find(I2(:, 1), 1, 'first');
        ShortList = List(I1(a, 2):I2(b, 2));
        Index3 = strcmp(CurStrToFind, ShortList);
        Soln3 = ShortList(Index3);
    end
    toc
    

    The output is:

    Elapsed time is 6.411537 seconds.
    Elapsed time is 1.396239 seconds.
    Elapsed time is 0.150143 seconds.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large collection of strings (up to 1M) alphabetically sorted. I have
I have a large array of data which I am displaying in a UITable.
I have large file comprising ~100,000 lines. Each line corresponds to a cluster and
I am developing an application which doesn't have large requirements for data storage. I
I have a large amount of files which I am trying to organize into
I have one voter table which contain large amount of data. Like Voter_id name
I have a large binary file that represents the alpha channel for each pixel
I have large number of test cases which runs with Spring Junit Support with
I have large text files upon which all kinds of operations need to be
I have large strings in NCLOB column and I'm allowing users to search in

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.