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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by