This loop cannot be parallelized. If flux_edge is a vector and not a matrix, accumarray would solve the problem efficiently. Try this:
sdl(selfs) = accumarray(selfs, sdl_edge);
resCell = splitapply(@(c) {sum(c, 2)}, flux_edge, selfs);
res(:, selfs) = cat(2, resCell{:});
The values of selfs are missing. Therefore I cannot test the code and I assume, it contains serious bugs. I assume you can find the remaining problems and modify the code until it solves your needs.
If the problem is time-critical (the bottleneck of the total program), I'd write a C-mex function. Accumulating in cells and joining them afterwards is not efficient for the memory consumption.
The size of selfs matters. It might be more efficient to collect the equal values at first by unique and run the loop over this list:
v = unique(selfs);
sdl = zeros(1, 7219);
res = zeros(5, 7219);
for iv = 1:numel(v)
av = v(iv);
mask = (selfs == av);
sdl(av) = sum(sdl_edge(mask));
res(:, av) = sum(flux_edge(:, mask), 2);
end
If this has a fair speed, you can parallelize it with parfor.
v = unique(selfs);
nv = numel(v);
A = zeros(1, nv);
B = zeros(5, nv);
parfor iv = 1:nv
av = v(iv);
mask = (selfs == av);
A(iv) = sum(sdl_edge(mask));
B(:, iv) = sum(flux_edge(:, mask), 2);
end
sdl = zeros(1, 7219);
sdl(v) = A;
res = zeros(5, 7219);
res(:, v) = B;
Best Answer