# how to make a matrix grow inside a parfor loop?

2 Ansichten (letzte 30 Tage)
kira am 7 Mai 2014
Kommentiert: kira am 9 Mai 2014
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.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Edric Ellis am 7 Mai 2014
Bearbeitet: Edric Ellis am 7 Mai 2014
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 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Edric Ellis am 8 Mai 2014
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 am 9 Mai 2014
hi, i tried my original code again, changing for by parfor an now it works... ?_?
thanks to all for your help...

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

Brian B am 7 Mai 2014
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 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
kira am 7 Mai 2014
i could this with S=sparse(fin,dim), but variable fin is very big (O(2^m))

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by