According to Mathworks' documentation, your implementation of the matrix Mat
is a sliced variable. That means you update different "slices" of the same matrix in different iterations, but the iterations do not affect each other. There is no data dependency between the loops. So you are ok to go.
Growing a
inside function foo
does not affect the parfor
, because a
is a regular variable located in foo
's stack. You can do anything with a
.
There do exist several issues to notice:
Don't use i
and j
as iteration counters
Defining i
or j
in any purpose is bad.
I have never been bored to refer people to this post - Using i and j as variables in Matlab.
Growing a
is bad
Every time you do a=[a;zeros(length(a),1)];
the variable is copied as a whole into a new empty place in RAM. As its size doubles each time, it could be a disaster. Not hard to imagine.
A lighter way to "grow" -
% initialize a list of pointers
p = 1;
cc = 1;
c{1} = zeros(1000,1);
% use it
while (go_on)
% do something
disp(c{cc})
....
p=p+1;
if (p>1000)
cc = cc+1;
c{cc} = zeros(1000,1);
p = 1;
end
end
Here, you grow a list of pointers, a cell array c
. It's smaller, faster, but still it needs copying in memory.
Use minimal amount of memory
Suppose you only need a small part of a
, that is, a(end-8:end)
, as the function output. (This assumption bases on the caller Mat(i,:)=foo();
where size(Mat, 2)=8
. )
Suppose err
is not related to previous elements of a
, that is, a(p-1)
, a(p-2)
, .... (I will loosen this assumption later.)
You don't have to keep all previous results in memory. If a
is used up, just throw it.
% if out of memory, don't allocate more; flush it
if (p>1000)
p = 1;
a = zeros(1000,1);
end
The second assumption may be loosen to that you only need a certain number of previous elements, while this number is already known (hopefully it's small). For example,
% if out of memory, flush it, but keep the last 3 results
if (p>1000)
a = [a(end-3:end); zeros(997,1)];
p = 4;
end
Trimming is not that complicated
% trim output after while loop
a(p+1:end)=[];
Proof:
>> a=1:10
a =
1 2 3 4 5 6 7 8 9 10
>> a(3:end)=[]
a =
1 2
>> a=1:10
a =
1 2 3 4 5 6 7 8 9 10
>> a(11:end)=[]
a =
1 2 3 4 5 6 7 8 9 10
>>
The reason is end
is 10
(although you can't use it as a stanalone variable), and 11:10
gives an empty array.