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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:54:05+00:00 2026-06-13T17:54:05+00:00

I have complex code blocks, in a Matlab script, that act on large, non-sparse

  • 0

I have complex code blocks, in a Matlab script, that act on large, non-sparse arrays. The code performs many write operations to random elements in the arrays, as well as read operations. The identical code must execute against different (large) arrays (i.e., the same code blocks, except for different array variable names).

I do not want to have long, duplicated code blocks that differ only in the array names.

Unfortunately, when I create a function to perform the operations, so that the code block appears only once, the performance slows down by a factor of 10 or more (presumably due to the copying of the array). However, I do not need the array copied. I would prefer to “pass by reference”, so that the purpose of the function call is ONLY to avoid having duplicated code blocks. There seems to be no way to avoid the copy-on-write semantics, however.

Also, it is impossible (so far as I understand) to create a script (not a function) to achieve this, because the script must contain identical variable names as the calling script, so I would need a different script for every array on which I wish to run the script, which gains nothing (I still would have duplicated code blocks).

I have looked into creating an alias variable name to “substitute” for the array variable name of interest, in which case I could call a script and avoid duplicated code. However, I cannot find any way to create an alias in Matlab.

Finally, I have attempted writing a function that utilizes the evalin() function, and passing the string name of the array variable to this function, but although this works, the performance is also vastly slower – about the same as passing the arrays by value to a function (at least a 10 times decay in performance).

I am coming to the conclusion that it is impossible in Matlab to avoid duplicating code blocks when performing complex operations on non-sparse arrays, in the effort to avoid the ghastly overhead that Matlab seems to present using any possible technique of avoiding duplicated code blocks.

I find this hard to believe, but I cannot find a way around it.

Does anybody know of a way to avoid duplicated code blocks when performing identical intricate operations on multiple non-sparse arrays in Matlab?

  • 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-13T17:54:07+00:00Added an answer on June 13, 2026 at 5:54 pm

    As noted by Loren on his blog, MATLAB does support in-line operations on matrices, which essentially covers passing arrays by reference, modifying them in a function, and returning the result. You seem to know that, but you erroneously state that because the script must contain identical variable names as the calling script. Here is code example that shows this is wrong. When testing, please copy it verbatim and save as a function:

    function inplace_test
    y = zeros(1,1e8);
    x = zeros(1,1e8);
    
    tic; x = compute(x); toc
    tic; y = compute(y); toc
    tic; x = computeIP(x); toc
    tic; y = computeIP(y); toc
    tic; x = x+1; toc
    end
    
    function x=computeIP(x)
    x = x+1;
    end
    
    function y=compute(x)
    y = x+1;
    end
    

    Time results on my computer:

    Elapsed time is 0.243335 seconds.
    Elapsed time is 0.251495 seconds.
    Elapsed time is 0.090949 seconds.
    Elapsed time is 0.088894 seconds.
    Elapsed time is 0.090638 seconds.
    

    As you see, the two last calls that use an in-place function are equally fast for both input arrays x and y. Also, they are equally fast as running x = x+1 without a function. The only important thing is that inside the function input and output parameters are the same. And there is one more thing…

    If I should guess what is wrong with your code, I’d say you made nested functions that you expect to be in-place. And they are not. So the below code will not work:

    function inplace_test
    y = zeros(1,1e8);
    x = zeros(1,1e8);
    
    tic; x = compute(x); toc
    tic; y = compute(y); toc
    tic; x = computeIP(x); toc
    tic; y = computeIP(y); toc
    tic; x = x+1; toc
    
        function x=computeIP(x)
            x = x+1;
        end
    
        function y=compute(x)
            y = x+1;
        end
    end
    
    Elapsed time is 0.247798 seconds.
    Elapsed time is 0.257521 seconds.
    Elapsed time is 0.229774 seconds.
    Elapsed time is 0.237215 seconds.
    Elapsed time is 0.090446 seconds.
    

    The bottom line – be careful with those nested functions..

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

Sidebar

Related Questions

I have quite a complex piece of code (JQuery and HTML5 Web SQL) that
We have a very complex Oracle package here (over 4,100 lines of code) that's
Trying to send a complex type between two systems that have the same code
We have the following code working for a complex rails form with checkboxes. I'm
I have code written in c, which contains some complex algorithms and I want
We have a complex app that serves AJAX JSON streams (using ADO to grab
Piggy backing off another question I posted , I have a complex find() that
I have a very complex Backbone application with many views/Models and collections. at times
Have a (rather complex) app that works fine on iOS 4 but fails on
I have a fairly complex piece of Javascript that works flawlessly with no errors

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.