Tips & Tricks

Hans Scharler

Avoid Reinventing the Wheel: Leverage MATLAB's Built-In Functions

Hans Scharler on 5 Mar 2024
Latest activity Reply by James on 26 Mar 2024

Most of my projects are the tension between what exists and what I must create to make the project work. The more I leverage what exists, the faster I can complete my project in a shorter amount of time. I often find out later in the process that a MATLAB function already exists and has existed for 10 releases. The faster that I discover built-in functions, the faster I can get to the new stuff that I have to invent.
I asked the AI Chat Playground why to use MATLAB's builtin functions and I got some interesting ideas. What are your thoughts?
AI Chat Playground:
  1. Efficiency and Performance: MATLAB's built-in functions are highly optimized for performance. They are written in optimized and compiled languages like C or C++, which means they run faster than equivalent functions written directly in MATLAB language. When you use these functions, your code can execute more efficiently, which is especially beneficial for handling large datasets or complex mathematical computations.
  2. Reliability and Stability: Built-in functions have been rigorously tested and debugged by the developers at MathWorks (the company behind MATLAB). They are less likely to contain errors compared to custom-written code, which means you can rely on them to produce accurate and consistent results.
  3. Ease of Use: Built-in functions are designed to be user-friendly, with clear documentation and support materials available. This can make it easier to perform complex operations without needing a deep understanding of the underlying algorithms. It also reduces the amount of code you need to write and debug, saving time and reducing the potential for errors.
  4. Compatibility: Built-in functions are updated alongside MATLAB itself, ensuring compatibility with new features and versions of the software. Using built-in functions can help ensure that your code continues to work correctly as MATLAB evolves.
  5. Advanced Features: MATLAB’s built-in functions often include advanced features and options that are not readily available or would be difficult to implement on your own. For example, MATLAB’s mathematical functions can handle a wide range of special cases and data types, providing more flexibility than custom implementations.
  6. Community and Support: Using built-in functions means you're working with standard tools recognized by the MATLAB community. This can make it easier to get help from other users or from MathWorks support, as more people will be familiar with the functionality and potential issues of these functions.
David Cazenave
David Cazenave on 10 Mar 2024
I have also discovered later that a MATLAB function or command already exists. The numbered list of information is valuable, I didn't know any of that stuff. I like the 'See Also' section at the bottom of function, or command, documentation pages. It lists the similar functions, or commands, related to the function or command the page is about.
goc3 on 6 Mar 2024
This is a good list.
One exception I do run into, though, pertains to point #1. Sometimes when an operation must be performed many times on known or controlled inputs, it is faster to code the simple case.
For example, if two scalars are compared to find the minimum—in a loop that will be run a million times—it is typically faster to code a simple if-else statement that compares the two values and assigns the minimum as opposed to using val = min(v1, v2):
if v1 < v2
val = v1;
val = v2;
This is not to say that min() is bad; it simply has enough overhead (e.g., checking for input sizes and types) that barebones code will outperform it if v1 and v2 are known to be scalars of a specific type.
(Of course, if such an operation can be vectorized, that is faster than using a loop. I am referring to cases wherein that is not possible, such as each i+1 element requiring the ith value for correct computation.)
James on 26 Mar 2024
Agreed 100% MATLAB's functions have a lot of checks to make sure they work 100% of the time. When you don't know for sure what the input will be, they are definitely useful and have saved my butt on a large number of occasions.
However, if you know exactly what the input to your function will be ("This input will always be scalar uint32 because it's coming from this function that returns a scalar uint32."), you can skip those input checks and customize your code to run faster. Assuming you do the proper error checking, of course...
Stephen23 on 5 Mar 2024 (Edited on 5 Mar 2024)
Overlapping somewhat, but I think warranting explicit mentions:
7. Where appropriate many inbuilt functions are already multithreaded inside. There are plenty of Answers threads where a user spends a considerable amount of time and effort to ultimately demonstrate that some inbuilt function or operation is already multi-threaded. Much astonishment ensues.
8. Careful selection of which inbuilt functions one uses allows code to be compiled into an executable or run on GPUs / clusters.
Hans Scharler
Hans Scharler on 5 Mar 2024
Great points. Thanks for adding on.

See Also