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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:53:04+00:00 2026-05-20T09:53:04+00:00

In each iteration of a loop, I am calculating a MATLAB matrix. These matrices

  • 0

In each iteration of a loop, I am calculating a MATLAB matrix. These matrices all must be concatenated together to create one final matrix. I know the dimensions of this final matrix before entering the loop, so I though preallocating the matrix using the ‘zeros’ function would be faster than initializing an empty array then simply appending the subarrays in each iteration of my loop. Oddly, my program runs MUCH slower when I preallocate. Here is the code (Only the first and last lines differ):

This is slow:

w_cuda = zeros(w_rows, w_cols, f_cols);

for j=0:num_groups-1

    % gets # of rows & cols in W. The last group is a special
    % case because it may have fewer than max_row_size rows
    if (j == num_groups-1 && mod(w_rows, max_row_size) ~= 0)
        num_rows_sub = w_rows - (max_row_size * j);    
    else
        num_rows_sub = max_row_size;
    end;

    % calculate correct W and f matrices
    start_index = (max_row_size * j) + 1;
    end_index = start_index + num_rows_sub - 1;

    w_sub = W(start_index:end_index,:);
    f_sub = filterBank(start_index:end_index,:);

    % Obtain sub-matrix
    w_cuda_sub = nopack_cu(w_sub,f_sub);

    % Incorporate sub-matrix into final matrix
    w_cuda(start_index:end_index,:,:) = w_cuda_sub;

end

This is fast:

w_cuda = [];

for j=0:num_groups-1

    % gets # of rows & cols in W. The last group is a special
    % case because it may have fewer than max_row_size rows
    if (j == num_groups-1 && mod(w_rows, max_row_size) ~= 0)
        num_rows_sub = w_rows - (max_row_size * j);    
    else
        num_rows_sub = max_row_size;
    end;

    % calculate correct W and f matrices
    start_index = (max_row_size * j) + 1;
    end_index = start_index + num_rows_sub - 1;

    w_sub = W(start_index:end_index,:);
    f_sub = filterBank(start_index:end_index,:);

    % Obtain sub-matrix
    w_cuda_sub = nopack_cu(w_sub,f_sub);

    % Incorporate sub-matrix into final matrix
    w_cuda = [w_cuda; w_cuda_sub];

end

As far as other potentially useful information–my matrix is 3D, and the numbers within it are complex. As always, any insight is appreciated.

  • 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-20T09:53:04+00:00Added an answer on May 20, 2026 at 9:53 am

    I have always assumed preallocation is faster for any array size and never actually tested it. So, I did a simple test timing the population of various array sizes from 1x1x3 up to 20x20x3 using 1000 iterations by both appending and preallocation methods. Here’s the code:

    arraySize = 1:20;
    numIteration = 1000;
    
    timeAppend = zeros(length(arraySize), 1);
    timePreAllocate = zeros(length(arraySize), 1);
    
    for ii = 1:length(arraySize); 
        w = [];
        tic;
        for jj = 1:numIteration
            w = [w; rand(arraySize(ii), arraySize(ii), 3)];
        end
        timeAppend(ii) = toc;
    end; 
    
    for ii = 1:length(arraySize); 
        w = zeros(arraySize(ii) * numIteration, arraySize(ii), 3);
        tic;
        for jj = 1:numIteration
            indexStart = (jj - 1) * arraySize(ii) + 1;
            indexStop = indexStart + arraySize(ii) - 1;
            w(indexStart:indexStop,:,:) = rand(arraySize(ii), arraySize(ii), 3);
        end
        timePreAllocate(ii) = toc;
    end; 
    
    figure;
    axes;
    plot(timeAppend);
    hold on;
    plot(timePreAllocate, 'r');
    legend('Append', 'Preallocate');
    

    And here are the (as expected) results:
    Comparison of array appending vs. preallocation

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

Sidebar

Related Questions

I'm trying to increase a number on each iteration of a for loop, in
With each loop iteration, I want to visually update the text of a textblock.
I need to form a string, inside each iteration of the loop, which contains
I would like to delay my while loop on each iteration. I tried the
Each of these variables has an integer value. But this syntax is not valid
I have a master shell script which called a child script for each iteration
all I referred to simpleMultiCopy.cu in CUDA SDK 4.0 and wrote one, see code
How do I save the iterations that have occurred in an xsl:for-each? (variables in
If I am iterating over each file using : @echo off FOR %%f IN
Each time a python file is imported that contains a large quantity of static

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.