How to pass a structure or a class object to functions by reference in Matlab [duplicate] - matlab

This question already has answers here:
Are Matlab Matrices transferred pass-by-value or pass-by-reference?
(2 answers)
Closed 6 years ago.
I have several large matrices that should be passed to different functions many times in an iterative algorithm. How can I avoid the unnecessary copy of variables to the function to speed up the program? Is there any way to group these matrices in a structure or class and pass them by reference or pointer?
a=zeros(1000,1000);
b=zeros(1000,1000);
d=myfunction(a,b);
a=myfunction2(b,d);
....
Thanks.
Update: I should have provided more details as I see in the comments. I have several large matrices. Few of them remain constant in the program and others change in each function at each iteration. So if I pass these matrices by value, Matlab makes a copy of that, changes the value and again copy them as the output of the function and then they go out of scope and get destroyed and at each iteration all these unnecessary copying make the program awfully slow. If I were to program it in C++, I would use object oriented programming for these matrices and I would pass them as reference to the function but I don't know if that's possible in MATLAB.

Ander's comment is absolutely true. That said, if you do need the functions to modify the variable contents, then you might want to think about refactoring the code to try to avoid this scenario. I know the code that you posted is just a simplified example, but you could look at that and ask questions like "do a and b need to be initialized outside of the function? Could the function initialize them and return them instead?".
If you really can't get around it, then you could encapsulate the data in a handle class and pass the handle class to the functions instead.

Related

How to index a function call for all objects in a vector? - for peformance improving

in a matlab script I have a vector which contains different instances of the same class. Now I would like to call the same function of each instance at the same time.
Is it, with tricky indexing, possible to do this WITHOUT a loop? For Example:
runner(1:100) = Human();
runner(:).run('fast'); % causes no error and no function call
In my project there is a vector with different neuronal networks (as objects). I tried to propagate all forward with one call.
Thanks for your time!
Well, there's one way if you're willing to modify your methods. What happens when you call something like runner(:).run('fast');? Rather than pass an instance of Human() to the run method 100 times it instead passes the entire vector of objects (an object array) to the method one time. Print out the object input from inside the method and see for yourself. Because nothing happens in your case, your run method must be written in such a way to ignore non-scalar objects. What you need to do is vectorize your methods. Depending on the nature of your methods, this may mean using for loops in some cases, but it will likely be the faster and more elegant option as it minimize the number of function calls.
Of course another option is to just call the run method 100 times using a for loop. If you really want "one call" then just hide the loop in a function or if you don't mind things to be a bit slower, use arrayfun which is nothing more than a function that hides a for loop:
arrayfun(#(obj)run(obj,'fast'),runner(:));

Save values of an array at Fortran 90 program

I have a two-dimensional and 1-D arrays of different variables of a system which characterize an Equilibrium state of the system from where the next configurations going to evolve from. Now i need to save the data in these arrays at the specific situation and proceed, so how should i write on the declaration (the syntax) of the main program and also in the subroutine which use these arrays to be delivered to them by adjustable-size array method of passing arrays?
Uuugh, sorry, but I'm not quite clear on what you are asking.
Are you interested in the SAVE statement, which enables a variable to retain its previous state, once for example, a subroutine is called again?

Forcing variable type in MATLAB

I have variables which are of double type I want them to be float. Is there a way to force a variable to be float and not double, also, Is there a way to do it in some global way for all functions and sub functions with few lines at the start of the main function? I have many functions and they use many temporary variables and create variables that they return. Going over all of my functions and changing them will be very difficult.
My rational for this request:
I'm writing the MATLAB program in order to simulate an algorithm which I'll then implement in hardware, I want to make sure that using 32bit as the size of my signals will not cause calculation errors.
Using B=single(A) as suggested by #cbz, or defining arrays as SINGLE, such as by calling B=zeros(3,3,'single') creates "floats" in Matlab.
There is no way to globally turn Matlab into a "float" environment. Although most lower-level functions are implemented for single as well (with a few exceptions, for example those mentioned in the help to DOUBLE), many high-level builtin functions will only work with double.
In other words, you'll have to manually define your variables as single, you'll have to periodically check that the variables haven't been quietly converted to double, and in the end, your code might not work if it needs a function that isn't implemented for single yet.
The MATLAB equivalent to 'float' is 'single. You can convert using
B = single(A).
That said, your assumption that this amounts to 32-bit might need revisiting. It's not as simple as that.

How to modify an array in function?

MATLAB is a pass by value language. I have a recursive function that processes pixel's neighbors. It is very expensive to make the copy of the image (in my case two images) each time the function is called.
I used global variables to solve the problem. Is there any other way to make a recursive function modify an array?
You have three options here, but maybe you don't need any of them, since Matlab used 'copy-on-write', i.e. variables are only copied if you modify them.
As #gnovice mentions, you can use a nested function. Variables used inside the nested function are shared between the nested function and the enclosing function. Nested functions are somewhat tricky to debug, and a bit more complicated to write/understand.
You can store your images as properties of a handle object, which is passed by reference.
You can write code differently in order to not use a recursive function, since Matlab isn't the best language for using those. If you have access to the image processing toolbox, you may be able to use functions like blockproc, or im2col to rewrite the function.
Finally, if you want to stay with your current scheme, I strongly suggest using persistent variables instead of globals.
MATLAB is not always pass-by-value, newer versions of MATLAB do pass-by-reference under some circumstances, see in-place operations and a more general discussion about MATLAB memory management in this SO post.
Without tail-call optimization it is inefficient to use recursion and MATLAB does not have it as far I know, but every recursion can be transformed into a loop.
If you make your recursive function a nested function within another function where the image data is stored, then the recursive function can modify the image data without needing to have it passed to it.
This is a common misconception. Although the sytanx of MATLAB is pass by value, it does not actually pass by value as in C. The interpreter is smart enough to only make copies when necessary. So you should just go ahead and pass by value and see if you run into memory problems.
As other posters have noted, you should try to avoid recursion in MATLAB anyway.

MATLAB tutorial for programmers [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm getting some new students soon, who will be writing MATLAB code. They're new to MATLAB, but they have experience coding in Java and C++.
I'm going to have them go through the Getting Started section of the MATLAB help. In addition, I want to give a small tutorial with the goal to prevent them from making some of the most common mistakes people make when switching to MATLAB (e.g. "MATLAB starts counting at 1"), and show them some features that they may not be aware of when coming from other languages (e.g. "you can subtract a scalar directly from an array, and for vectors, there's bsxfun").
What are the most important things I should tell them?
I agree with previous answers, but I'd say indexing is the first and the most important and complex concept in studying MATLAB. I saw many C programmers starting with MATLAB just write loops, a lot of loops, something ridiculous like
for i=1:10
a(i)=i;
end
instead of simple a=1:10;.
So I'd suggest them to read about matrix programming concepts:
How to create simple vectors and matrices
Which variables can be used for indexing
How to create and apply indexes
Logical operations and functions, logical and numeric indexes (find function)
Indexing right and left side of expression
Difference between indexing numerical matrices and cell arrays
How to use indexes as output from different functions, like sort, unique, ismember, etc.
You cannot apply indexes to intermediate results
As for productivity, I would add that knowing how to use editor's cell mode is very useful.
Enough snippy comments, here's something of an answer too:
The Matlab desktop: what all the windows are for, dragging code from the history back into the command window, the variable inspector, etc.
Plotting: not just the plot command, but how to use the plot GUI tools, and how to create an M-file from a graphic.
M-files for scripts and functions, and the key differences between them.
M-Lint, the profiler.
Use Matlab as a vehicle for teaching the perils and pitfalls of floating-point arithmetic.
Getting help: at the command line, on the web, documentation, the file exchange, ...
Set path and the current working directory.
Importing data from files, exporting data to files, loading and saving.
That should be enough to keep them busy for an hour or so.
To clarify, I propose these topics to help you teach your students to avoid common Matlab errors including;
Unproductive use of the tool, retyping commands which can easily be recalled from the history, using C (or Java) style file reading commands instead of uuimport, slowly typing scripts to draw graphics when Matlab can do it for you, wondering what all the little orange lines in the editor right margin mean and the squiggly underlines, trying to figure things out for themselves when the help facilities could tell them, tons of other stuff that many much more experience Matlab users have taken ages to learn.
Floating point arithmetic is not real.
and probably a lot of other stuff too.
For those coming from C-family languages, the element-wise operators are new. It took me a couple of months to discover the ./ and .* operators. Before that, I used to write for loops for element-wise operations. So perhaps that's something that should be pointed out.
With respect to unexpected or non-intuitive MATLAB features that may cause them confusion, there are some good pointers in this question:
Corner Cases, Unexpected and Unusual MATLAB
With respect to cool time-saving/efficiency tricks, this other question has some nice examples:
What are your favourite MATLAB/Octave programming tricks?
And for a few potentially more advanced topics, you can refer to the answers to this question:
MATLAB interview questions?
Now for my $0.02. Based on the sorts of questions I've seen asked most frequently on SO, I'd say you will want to make sure they have a good understanding of the following concepts:
Reading and writing data files of different formats, such as using CSVREAD, DLMREAD, TEXTREAD, FREAD, FSCANF, LOAD, and all their write equivalents.
How to deal effectively with cell arrays.
The different image formats, how these are represented, and how to modify them (which will involve a discussion of various data types and how to deal with multi-dimensional arrays).
How to use handle graphics to control the appearance of various graphics objects.
And here are some neat features that are already implemented in MATLAB that may save them some time and effort:
Functions for performing various array operations, like KRON, DIAG, and TRIU.
Functions to create specialized matrices, like HANKEL and TOEPLITZ.
Predefined dialog boxes, like UIGETFILE and INPUTDLG.
MATLAB is conceptually in some ways very different from other languages you mentioned:
cells are used were Java uses upcasting
global and persistent variables are static in Java
gui handles being just numbers of type double
nested functions are closures, neither Java nor C/C++ has such feature
seldom used private and #TYPE folders for visibility scoping
array handling tricks
very easy interoperability with Java/COM/.Net using MATLAB syntax
variadic function arguments, handling of function arguments with varargin / varargout
memory management