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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:00:01+00:00 2026-05-26T04:00:01+00:00

In Matlab, this type of algorithm (growing arrays) is advised against mine = []

  • 0

In Matlab, this type of algorithm (“growing arrays”) is advised against

mine = []
for i=1:100,
    mine = [mine,randn(1)]
end

whereas it seems that many examples for Python show this kind of algorithm (this is a really bad example though):

import numpy.random as rand

mine = []
for i in range(100):
    mine.append(rand.random(1)[0])

I wonder why that is — what is the difference?

  • 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-26T04:00:02+00:00Added an answer on May 26, 2026 at 4:00 am

    The difference is that:

    • In MATLAB, every iteration of your loop re-allocates the matrix to increase the size by one and copies the entire contents into the newly allocated space.
    • Python lists don’t work like that. More space is allocated than is needed at any given point and this allocated space grows in a manner that guarantees that appends are done in amortized constant time.

    That said, I think the difference is largely cultural:

    • It is common to have large numeric matrices in MATLAB, and growing such matrices one element (or one row/column) at a time would indeed be expensive.
    • On the other hand, no one would use a Python list (or a list of lists) to represent a large matrix: that would be very slow and would make very poor use of memory. Numerical Python‘s ndarray would be used instead, and ndarray would offer exactly the same tradeoffs as a MATLAB matrix.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write a Matlab script that does this: The input is 2
I have an array created in MATLAB that contains a number of cell type
I am plotting a financial candlestick chart using this MATLAB function: http://www.mathworks.com/help/toolbox/finance/candlefts.html How do
I'm fairly sure there should be an elegant solution to this (in MATLAB), but
I keep getting this error in Matlab: Attempted to access r(0,0); index must be
Using MATLAB, I have this code: value = input('>> Enter a value: '); and
This question kind of starts where this question ends up. MATLAB has a powerful
My knowledge of matlab is merely on a need to know basis, so this
I am trying to sub-array in MATLAB with no luck. This is what I
When I run this code: >> I = imread('D:\Works\matlab\SecCode.php.png','png'); >> imshow(I); It always shows

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.