Variable loop depth - Make into Matrix?

1 次查看(过去 30 天)
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 个评论
Matt Sprague
Matt Sprague 2018-1-11
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;

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by