Sliced Variables in N Choose K Search
Show older comments
I am trying to generate computations for a problem of the form N Choose K. The function is computational expensive, so I would like to use parallel computing. Using a preallocated array and sliced variables, originally my code looked like this:
A = NaN(100,100);
parfor x = 1:100
for y = x+1:100
A(x,y) = somefunction(x,y)
end
end
When I try to run I get the following error:
Error: When indexing the sliced variable 'A', the range of the for-loop variable 'y' must be a row vector of positive constant numbers or variables. For more information, see Parallel for Loops in MATLAB, "Nested for-Loops with Sliced Variables".
The problem is the y variable as it is not constant. To solve this issue, I changed the code to:
A = NaN(100,100);
parfor x = 1:100
for y = 1:100
if y > x
A(x,y) = somefunction(x,y)
end
end
end
Which it now works. However, it seems to me that having to run the check y > x so many times is very inefficient.
Would there be a better way?
2 Comments
Andres Morales
on 15 Nov 2022
Bruno Luong
on 16 Nov 2022
IMO the if test cost is negligible.
Accepted Answer
More Answers (0)
Categories
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!