Variable loop depth - Make into Matrix?

1 回表示 (過去 30 日間)
Douglas Anderson
Douglas Anderson 2018 年 1 月 7 日
コメント済み: Matt Sprague 2018 年 1 月 11 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by