How can I store an array from a called function to another array?
4 views (last 30 days)
Show older comments
I have a function Sysmodel(), which calculates the value of an array B based on random variables. What I want is to store different values of B to later take an average, something like monte carlo simulations. When I try to do this, I only get an array of zeros with the last row filled with B from the last iteration. My code is this:
for i = 1:1000
Sysmodel()
A(i,:)=B;
end
Could you point out where I am going wrong? Or give me a better way to carry out these simulations?
3 Comments
Image Analyst
on 19 Apr 2015
Why do you say "the value of an array B"? Normally an array is multiple values, not a single value. Did you really mean to say "the values of an array B"?
What are the dimensions of A and B? How does B change in your loop? Is B a global variable that is declared global both in that routine, and in Sysmodel()??? And you change the value of the global variable B in Sysmodel? If B is declared only in your code you posted, then it will have the same value every time, unless you've intentionally left out critical parts of your code.
Answers (1)
Geoff Hayes
on 19 Apr 2015
Kirtiman - if the dimension of B changes from iteration to iteration, then you will want to use a cell array to store the data from each iteration. Let
A = {};
and then update A as
for i = 1:1000
B = Sysmodel();
A{i} = B;
end
As for too many zeros, I feel that you have not shown all of the relevant code. Will A always be updated or only under certain conditions? For example, if the first update to A is on the 267th iteration of the for loop then
A(267,:) = randi(255,1,268);
will set A to be a matrix that is 267x268 with all rows except the last equal to zero.
2 Comments
Geoff Hayes
on 19 Apr 2015
So B is made up of four arrays? I think that you may have generalized your example a little too much... ;) If I run your Sysmodel function (taking some liberties with a couple of the statistics functions that I don't have) then I see that these four arrays are each 1x3. Is this correct? And if so, how do you build B from these four?
You may need to step through your code to see why A changes on subsequent iterations. See debugging in MATLAB for details. Because if you do, I think that you will find that A is cleared on every iteration of the for loop. This is because the first line in Sysmodel is to clear all variables
clear all;
and that this will include the A that has been previously declared. So it is getting cleared on each iteration so that when you reach last iteration the last element of A is set to B and all other elements prior to this one are set to zero. This can be observed with a quick example
A = [];
A(5,1:5) = randi(255,1,5)
which creates
A =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
193 153 37 12 221
And that is exactly what you are seeing! I suggest that you convert your Sysmodel.m from a script to a function with a signature like
function [temp, APratH, APratF, APratHD] = Sysmodel()
and remove the clear all statement. This way, your A is preserved and you avoid any collisions in the workspace because of the script. See Scripts and Functions for the differences between the two.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!