How can I store an array from a called function to another array?

4 views (last 30 days)
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
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.
Kirtiman
Kirtiman on 19 Apr 2015
Edited: Kirtiman on 19 Apr 2015
@Geoff Hayes, Sysmodel basically, creates B and no, size of B changes randomly. Sorry I didn't mention that before.
@Image Analyst, Yes I meant the set of values of array B obtained while running Sysmodel, which by the way change every time. Values of B and the size of B are selected randomly,the size is 1xsomenumber. No B is not a global variable, it is created by Sysmodel. I have not posted the code for Sysmodel, is it important that I do that? Since the code is pretty long.
Also if it helps, what happens is lets say B is 1x268, then what i get is the latest values of B at the row 267 in A. The size of A is then 267x268, and except 267th row, all other rows have 0s as their elements.
If i do this:
clear all
Sysmodel();
x=APratH;
Sysmodel();
y=APratH;
y gets created but not x, why could that be?

Sign in to comment.

Answers (1)

Geoff Hayes
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
Kirtiman
Kirtiman on 19 Apr 2015
Edited: Kirtiman on 19 Apr 2015
No, B changes every time I run Sysmodel, also even after I run your above code, I get the same result as mentioned, i.e. the last cell contains the array while others contains empty arrays. Attaching code, as asked.
Also the 'B' in this case are three arrays, APratH, APratF, APratHD and temp. I need to run this code many times to store different values of these arrays.
Also I need to take an avarage of these arrays values after about 1000 simulations.
Geoff Hayes
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.

Sign in to comment.

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!