Variable loop depth - Make into Matrix?
1 view (last 30 days)
Show older comments
Hello,
I have a situation where I am trying to make a comb function for each of a series of variables. The code I have thus far is as follows
for row_delay = 1:num_row_dealays
for hole delay = 1: num_hole_delays
for row_num = 1:num_rows
for hole_num = 1:num_holes
if (design_array(row_num,hole_num)) % Array has either 1 or 0
ftime = hole_delays(hole_delay) * hole_num + row_delays(row_delay) * (row_num-1); % hole_delays predefined
comb_array(row_delay,hole_delay,ftime) = comb_array(row_delay,hole_delay) + 1;
end
end
end
end
end
This works, but now I need to have a variety of depths (number of different "row_delays" arrays).
Can this kind of loop be made into a matrix operation. Clearly, the "comb_array" will have to have different arguments, like "row_delay1", "row_delay2", and the number of them will vary, so simplifying this would be great!
Thanks! Any suggestions are welcome.
Doug Anderson
1 Comment
Matt Sprague
on 11 Jan 2018
You can use logical indexing to replace for loops. It's hard to fully understand what is happening in the "comb_array" line since you are referring to the variable on the left side of the equation with 3 dimensions but on the right side only with 2 dimensions. However, the optimized code should look something along the lines of:
row_delay = 1:num_row_dealays;
hole_delay = 1: num_hole_delays;
row_num = 1:num_rows;
hole_num = 1:num_holes;
%
x = design_array(row_num,hole_num); % Array has either 1 or 0
ftime = hole_delays(hole_delay(x)) .* hole_num(x) + row_delays(row_delay(x)) .* (row_num(x)-1); % hole_delays predefined
%
comb_array(row_delay(x),hole_delay(x),ftime) = comb_array(row_delay(x),hole_delay(x)) + 1;
Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!