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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:28:16+00:00 2026-05-15T14:28:16+00:00

Is there an easy way to find a smaller cell array of strings within

  • 0

Is there an easy way to find a smaller cell array of strings within a larger one? I’ve got two lists, one with unique elements, and one with repeating elements. I want to find whole occurrences of the specific pattern of the smaller array within the larger. I’m aware that strcmp will compare two cell arrays, but only if they’re equal in length. My first thought was to step through subsets of the larger array using a loop, but there’s got to be a better solution.

For example, in the following:

smallcellarray={'string1',...
                'string2',...
                'string3'};
largecellarray={'string1',...
                'string2',...
                'string3',...
                'string1',...
                'string2',...
                'string1',...
                'string2',...
                'string3'};

index=myfunction(largecellarray,smallcellarray)

would return

index=[1 1 1 0 0 1 1 1]
  • 1 1 Answer
  • 1 View
  • 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-05-15T14:28:17+00:00Added an answer on May 15, 2026 at 2:28 pm

    You could actually use the function ISMEMBER to get an index vector for where the cells in largecellarray occur in the smaller array smallcellarray, then use the function STRFIND (which works for both strings and numeric arrays) to find the starting indices of the smaller array within the larger:

    >> nSmall = numel(smallcellarray);
    >> [~, matchIndex] = ismember(largecellarray,...  %# Find the index of the 
                                    smallcellarray);    %#   smallcellarray entry
                                                        %#   that each entry of
                                                        %#   largecellarray matches
    >> startIndices = strfind(matchIndex,1:nSmall)  %# Starting indices where the
                                                    %#   vector [1 2 3] occurs in
    startIndices =                                  %#   matchIndex
    
         1     6
    

    Then it’s a matter of building the vector index from these starting indices. Here’s one way you could create this vector:

    >> nLarge = numel(largecellarray);
    >> endIndices = startIndices+nSmall;  %# Get the indices immediately after
                                          %#   where the vector [1 2 3] ends
    >> index = zeros(1,nLarge);           %# Initialize index to zero
    >> index(startIndices) = 1;           %# Mark the start index with a 1
    >> index(endIndices) = -1;            %# Mark one index after the end with a -1
    >> index = cumsum(index(1:nLarge))    %# Take the cumulative sum, removing any
                                          %#   extra entry in index that may occur
    index =
    
         1     1     1     0     0     1     1     1
    

    Another way to create it using the function BSXFUN is given by Amro. Yet another way to create it is:

    index = cumsum([startIndices; ones(nSmall-1,numel(startIndices))]);
    index = ismember(1:numel(largecellarray),index);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.