이 코드를 작성하고 잘 작동하지만 가능하면 두 for 루프를 제거하여 최적화하고 싶습니다. 아무도 내가 어떻게 이것을 달성 할 수있는 아이디어가 있습니까? 많은 감사합니다! 외부 루프를 제거Vectorising 중첩 For 루프
for k = 1:10
for l = 1:num
L_Mid(k,l) = sum(LMat(k,1+in_point(l):out_point(l)));
L_Sides(k,l) = sum(LMat(k,:))-L_Mid(k,l);
end
end
첫 번째 단계 :
chosen_runs = [2:5];
% Configurations for each test in order
XY = [0 1; 0 1; 0 9; 0 1; 0 2; 0 3; 0 4; 0 5; 11 12; 11 12];
% Inductance Matrix
LMat = [0.0045 0.0045 0.0045 0.0044 0.0044 0.0044 0.0044 0.0044 0.0043 0.0043;
0.0045 0.0046 0.0046 0.0045 0.0045 0.0045 0.0045 0.0044 0.0044 0.0044;
0.0045 0.0046 0.0046 0.0046 0.0046 0.0046 0.0045 0.0045 0.0045 0.0045;
0.0044 0.0045 0.0046 0.0047 0.0047 0.0047 0.0046 0.0046 0.0046 0.0046;
0.0044 0.0045 0.0046 0.0047 0.0048 0.0048 0.0047 0.0047 0.0047 0.0046;
0.0044 0.0045 0.0046 0.0047 0.0048 0.0048 0.0048 0.0048 0.0048 0.0047;
0.0044 0.0045 0.0045 0.0046 0.0047 0.0048 0.0049 0.0049 0.0049 0.0048;
0.0044 0.0044 0.0045 0.0046 0.0047 0.0048 0.0049 0.0050 0.0049 0.0049;
0.0043 0.0044 0.0045 0.0046 0.0047 0.0048 0.0049 0.0049 0.0050 0.0050;
0.0043 0.0044 0.0045 0.0046 0.0046 0.0047 0.0048 0.0049 0.0050 0.0051];
% Declaration of Variables
runs = chosen_runs;
num = length(runs);
in_point = zeros(num,1);
out_point = zeros(num,1);
L_Mid = zeros(10,num);
L_Sides = zeros(10,num);
%%%%%%%%%%%%%%%%%%%%%%%%%%
in_point = XY(runs,1); % Creates a column vector each row of which is the in_point for a chosen run
out_point = XY(runs,2); % Creates a column vector each row of which is the out_point for a chosen run
in_point
out_point
for k = 1:10
for l = 1:num
L_Mid(k,l) = sum(LMat(k,1+in_point(l):out_point(l))); % Creates a matrix, each column of which is the inductance (in between current leads) for a chosen run, each row is a different layer in the solenoid.
L_Sides(k,l) = sum(LMat(k,:))-L_Mid(k,l); % Creates a matrix, each column of which is the inductance (either side of the current leads) for a chosen run, each row is a different layer in the solenoid.
end
end
L_Mid
L_Sides
[짧은 예제] (http://sscce.org/)로 최소화 할 수 있습니까? –