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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:49:31+00:00 2026-06-16T11:49:31+00:00

According to this question , I should try to use Preallocation is Matlab. Now

  • 0

According to this question, I should try to use Preallocation is Matlab.

Now I have a situation that I cannot calculate the exact size of the matrix to preallocate. I can guess the size.

suppose the actual size of the matrix is 100, but I don’t know it. Sh

Which scenario is more efficient:

  1. Should I be lavish? I guess a large matrix and at the end I remove extra rows.
  2. Should I be stingy? I guess a small size and If it was wrong, I add new rows.

Thanks.

  • 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-16T11:49:33+00:00Added an answer on June 16, 2026 at 11:49 am

    To my opinion, the answer is a bit more complex than portrayed by @natan.
    I think there are two factors his answer does not take into account:

    1. Possible necessary copies of memory: when you under-estimate a matrix size and you re-allocate it, all its old values should be copied to the new allocated location.

    2. Continuity of memory chunks: sometimes Matlab is able to allocate new memory continuously at the end of the old matrix. In principle, in such a scenario the old values need not be copied to the new location – since it is the same as the old one just bigger. However, if you add rows to a 2D matrix, the content needs to be copied even in this scenario, since Matlab stores matrices in a row-major fashion in memory.

    So, my answer is this:

    First of all, what exactly don’t you know about the size of the matrix: if you know one dimension – make it the number of rows of your matrix, so you’ll only need to change the number of columns. This way, if your already stored data needs to be copied, it would be copied at larger chunks.

    Second, it depends on how much free RAM you have at your disposal.
    If you are not short at RAM, then there’s nothing wrong with over estimating.

    However, if you are short at RAM, consider under estimating. BUT when you re-allocate, increase the new block size at each iteration:

    BASIC_SIZE = X;  % first estimate
    NEW_SIZE = Y;    % if need more, add this amount
    factor = 2;      
    arr = zeros( m, BASIC_SIZE ); % first allocation, assuming we know number of rows
    while someCondition
        % process arr ...
        if needMoreCols
            arr(:, size(arr,2) + (1:NEW_SIZE) ) = 0; % allocate another block
            NEW_SIZE = round(NEW_SIZE * factor);  % it seems like we are off in estimation, try larger chunk next time factor should be > 1
        end
    end
    arr = arr(:, 1:actualNumOfCols ); % resize to actual size, discard unnecessary columns
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to this question I succeeded to create upload image, but now I need
I've installed Python via MacPorts. According to this question, the files in /opt/local/bin should
According to this question ( How should I handle product upgrades in a WiX
According to this question it seems like you can do this for Methods. What
According to this question in order for Flymake to work you must add a
I have made a custom suface button according this example: <Button> <Button.Template> <ControlTemplate TargetType=Button>
Writing another question for SO, I came to a pattern that I use very
According to this MSDN article , you should not catch general exceptions. I'm sure
This question is similar to another one, concerning pyCURL I have the following error
This question indicates that it is possible for a 32-bit COM client to talk

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.