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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:49:14+00:00 2026-05-15T20:49:14+00:00

Possible Duplicate: Growable data structure in MATLAB So in my current MATLAB script, I

  • 0

Possible Duplicate:
Growable data structure in MATLAB

So in my current MATLAB script, I have a very large indeterminate-sized growing array. There is currently nothing I can do about it, because if I actually preallocate, it would take many many many times more memory than it should need (maximum possible amount of values is 640 per pixel, but usually it’s something along the lines of 2-5).

Usually in this case, I’d be using a vector in C++ or something, where it grows exponentially in relation to a given capacity. But I think the matrices in Matlab starts fragmenting much faster than the purpose driven C++ vectors.

What do you guys think is the best alternative to something like this? Or should I just stick with normal arrays and hope that adding around 100k elements sequentially will work?

Thanks in advance.

  • 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-15T20:49:14+00:00Added an answer on May 15, 2026 at 8:49 pm

    You could try what std::vector does when reallocating elements — double its capacity every time it fills up which has an amortized cost of O(1).

    Test:

    function test_MAT_vector
    
        LIM = 5e4;
        %%# Normal
        tic;
        A = zeros(1,1);
        for i = 1:LIM
            A(end+1) = i;
        end
        toc
    
        %%# std::vector clone
        tic;
        B = zeros(1,1);
        for i = 1:LIM
            if(i > numel(B))
                B = [B;zeros(numel(B),1)];
            end
            B(i) = i;
        end
        toc
    
    end
    

    Output:

    Elapsed time is 3.489820 seconds.

    Elapsed time is 0.006710 seconds.

    Using cell

    %%# cell
    tic;
    A = cell(1,1);
    for i = 1:LIM
        A(end+1) = {i};
    end
    toc
    

    Elapsed time is 2.740792 seconds.

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

Sidebar

Related Questions

Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: array_splice() for associative arrays How to add an array value to the
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: Can a Bash script tell what directory it's stored in? Is there
Possible Duplicate: Matlab gives wrong answer Can anyone explain to me why the following

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.