how to make a matrix grow inside a parfor loop?

Hi, i would like to do this in a parfor loop:
S=[];
for n=in:fin
[s,trueorfalse]=somefunction(someinput);
if trueorfalse
S=[S;s];
end
end
I cannot preallocate because i don't know how much big S will be. I tried some thing but nothing worked so far.
Hope you could help me.

 채택된 답변

Edric Ellis
Edric Ellis 2014년 5월 7일
편집: Edric Ellis 2014년 5월 7일

0 개 추천

You should be able to do this. For example, the following works correctly:
S = [];
parfor idx = 1:10
r = rand();
if r > 0.5
S = [S, r];
end
end

댓글 수: 3

kira
kira 2014년 5월 7일
that is exactly what i tried, but i get an error, about the way S is used.
Did you try precisely that code? That code works correctly all the way back even to MATLAB R2008a. Which version did you try it in?
kira
kira 2014년 5월 9일
hi, i tried my original code again, changing for by parfor an now it works... ?_?
thanks to all for your help...

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Brian B
Brian B 2014년 5월 7일

1 개 추천

You could make S a cell array in the loop, then create a matrix afterward:
somefunction = @(x)deal(x,rand(1)<0.3);
S=cell(100,1);
parfor n=1:100
[s,trueorfalse]=feval(somefunction, n);
if trueorfalse
%S=[S;s];
S{n} = s;
end
end
S = cell2mat(S);

댓글 수: 1

kira
kira 2014년 5월 7일
i could this with S=sparse(fin,dim), but variable fin is very big (O(2^m))

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2014년 5월 7일

댓글:

2014년 5월 9일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by