Search code examples
matlabmatlab-compiler

How can I use Parallel loop in Matlab


If I have the following code in Matlab, How can I fix the indexing in st3 matrix in order to perform parallel loop? Thank you

n=1;
parfor j=1:10
[~,x1]=compare2Arrays(st1,st2);
    if isempty(x1)
        st3(n)=st4(j);
        n=n+1;
    end
end

Solution

  • Since the loops doesn't run in sequence. You cant use n like that. Here is the updated code.

    n=1;
    st5=nan(1,10);
    parfor j=1:10
    [~,x1]=compare2Arrays(st1,st2);
        if isempty(x1)
            st5(j)=st4(j);
        end
    end
    st3=st5(not(isnan(st5)));