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

  • Home
  • SEARCH
  • 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 7061621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:29:48+00:00 2026-05-28T04:29:48+00:00

I have a cell array in Matlab: strings = {‘one’, ‘two’, ‘three’}; How can

  • 0

I have a cell array in Matlab:

strings = {'one', 'two', 'three'};

How can I efficiently calculate the length of all three strings? Right now I use a for loop:

lengths = zeros(3,1);
for i = 1:3
    lengths(i) = length(strings{i});
end

This is however unusable slow when you have a large amount of strings (I’ve got 480,863 of them). Any suggestions?

  • 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-05-28T04:29:48+00:00Added an answer on May 28, 2026 at 4:29 am

    You can also use:

    cellfun(@length, strings)
    

    It will not be faster, but makes the code clearer.
    Regarding the slowness, you should first run the profiler to check where the bottleneck is. Only then should you optimize.

    Edit: I just recalled that ‘length’ used to be a built-in function in cellfun in older Matlab versions. So it might actually be faster! Try

     cellfun('length',strings)
    

    Edit(2) : I have to admit that my first answer was a wild guess. Following @Rodin s comment, I decided to check out the speedup.

    Here is the code of the benchmark:

    First, the code that generates a lot of strings and saves to disk:

    function GenerateCellStrings()
        strs = cell(1,10000);
        for i=1:10000
            strs{i} = GenerateRandomString();
        end
        save strs;
    end
    
    function st = GenerateRandomString()
        MAX_STR_LENGTH = 1000;
        n = randi(MAX_STR_LENGTH);
        st = char(randi([97 122], 1,n ));
    
    end
    

    Then, the benchmark itself:

     function CheckRunTime()
        load strs;
        tic;
        disp('Loop:');
        for i=1:numel(strs)
            n = length(strs{i});
        end
        toc;
    
        disp('cellfun (String):');
        tic;
        cellfun('length',strs);
        toc;
    
        disp('cellfun (function handle):');
        tic;
        cellfun(@length,strs);
        toc;
    
    end
    

    And the results are:

    Loop:
    Elapsed time is 0.010663 seconds.
    cellfun (String):
    Elapsed time is 0.000313 seconds.
    cellfun (function handle):
    Elapsed time is 0.006280 seconds.

    Wow!! The ‘length’ syntax is about 30 times faster than a loop! I can only guess why it becomes so fast. Maybe the fact that it recognizes length specifically. Might be JIT optimization.

    Edit(3) – I found out the reason for the speedup. It is indeed recognition of length specifically. Thanks to @reve_etrange for the info.

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

Sidebar

Related Questions

I have three arrays, all the same size: xout % cell array xin %
You have: val array = new Array[Array[Cell]](height, width) How do you initialize all elements
I have this cell array in MATLAB: y = { 'd' 'f' 'a' 'g'
I am writing a cell array of string into Excel from Matlab. I have
I have an array created in MATLAB that contains a number of cell type
I have a cell array in matlab columns = {'MagX', 'MagY', 'MagZ', ... 'AccelerationX',
i have a cell array with elements 22*22,18*18*6,14*14*6,10*10*6,6*6*6.can u please help me to initialize
I have a loop in MATLAB that fills a cell array in my workspace
So, I have a cell-array of 1xN vectors of different lengths. I want to
I have a cell array, x, and each row of which is a string

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.