Search code examples
matlabparfor

Parfor inside for loop


I have the following short Matlab code:

res = cell(10*100,1);
for i = 1:10
    parfor j = 1:100
        idx = ((i-1) * 100) + j;
        res(idx) = 5; 
    end
end

I get an error for res(idx) = 5;. If I don't use the variable i in the parfor loop it works but I have to keep track of i.

How can I do it?

Edit: I have solved it.

res = zeros(10*100,1);
for i = 1:10
    temp = zeros(100,1);
    parfor j = 1:100
        a = i;
        temp(j) = data((i-1) * 100) + j);
    end
    res((i-1)*100+1:i*100) = temp;
end

Solution

  • would

    spmd
    res = zeros(10*100,1);
    for i = 1:10
        for j = 1:100
            idx = ((i-1) * 100) + j;
            res(idx) = 5; 
        end
    end
    end
    

    solve your problem?