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?
Related
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.
I would like to know whether there is a way to reduce the amount of memory used by the following piece of code in Matlab:
n=3;
T=100;
r=T*2;
b=80;
BS=1000
bsuppostmp_=cell(1,BS);
bslowpostmp_=cell(1,BS);
bsuppnegtmp_=cell(1,BS);
bslownegtmp_=cell(1,BS);
for w=1:BS
bsuppostmp_{w}= randi([0,1],n*T,2^(n-1),r,b);
bslowpostmp_{w}=randi([0,3],n*T,2^(n-1),r,b);
bsuppnegtmp_{w}=randi([0,4],n*T,2^(n-1),r,b);
bslownegtmp_{w}=randi([0,2],n*T,2^(n-1),r,b);
end
I have decided to use cells of matrices because after this loop I need to call separately each single matrix in another loop.
If I run this code I get the message error "Your system has run out of application memory".
Do you know a more efficient (in terms of memory) way to store each single matrix?
Let's refer the page about Strategies for Efficient Use of Memory:
Because simple numeric arrays (comprising one mxArray) have the least overhead, you should use them wherever possible. When data is too complex to store in a simple array (or matrix), you can use other data structures.
Cell arrays are comprised of separate mxArrays for each element. As a result, cell arrays with many small elements have a large overhead.
I doubt that the overhead for cell array is really large ...
Let me give a possible explanation. What if Matlab cannot use the swap file in case of storing the 4D arrays into a cell array? When storing large numeric arrays, there is no out-of-memory error because Matlab uses the swap file for caching each variable when the used memory becomes too big. Whereas if each 4D array is stored in a super cell array, Matlab sees it as a single variable and cannot fragment it part in the RAM and part in the swap file. Ok I don't work at Mathworks so I don't know if I'm right or not, it's just an idea about this topic so I would be glad to know what is the real reason.
So my advice is the same as other comments: try to free matrices as soon as you've done with them. There is not so many possibilities to store many dense arrays: one big array (NOT recommended here, will reach out-of-memory sooner because Matlab makes it contiguous), cell array or struct array (and if I correctly understand the documentation, the overhead can be equivalent). In all cases, the data amount over all 4D arrays is really large, so the best thing to do is to care about keeping the memory constantly as low as possible by discarding some data once they are used and keep in memory only the results of computation (in case they take lower memory usage ...).
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(:));
I want to store a character along with numbers ? Is using Cells the only way ?
Yes, unless you store the ASCII values but I don't think it would be very useful.
Edit: Or an array of structures?
a.num = [1 2 3]
a.char = 'A'
I don't know exactly what you are trying to achieve…
This is a classic Computer Science 101 sort of question. An array holds 1 type of data traditionally. In matlab the term gets abused.
Here are some things to know:
An array of characters is called a string
An array can only store one data type
The size of an array can’t change
But matlab has an abstraction on top of all this so the engineer that didn't study programming for a year can still get the job done. Whilst matlab lets you change the size of a 1D matrix, it still won't let you have different types of data in the same array. Keep in mind that matlab 1D arrays aren't strictly arrays because this fact. Similarly with arrays of arrays with differing in size. Matlab doesn’t allow different data structures for optimization reasons.
This question arises from not knowing the containers which are available.
List: A container indexed elements (great for sorting and adding elements quickly)
Set: for a collection of unique elements (great for ensuring that there are no duplicates)
Map: Great for quickly retrieving elements based on a unique identifier
Java has implementations for these and you can use these within matlab if you want which is the general way it is done if you need a collection other than a matrix. I don’t think matlab bothered to wrap these classes themselves because they would be exactly the same anway.
In general its not a great idea to store different data types in these collections if you can avoid it, do so but otherwise so be it.
PS I don't think structs should ever be used because there is no way to know what members they have without debugging them.
If you do
a.num = [1 2 3]
a.char = 'A'
Unless you tell everyone a.num and a.char exist there is no way of knowing that a has char and num, without running code. Bad bad practice.
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.