Matlab 嵌套循环的矢量化

Matlab 嵌套循环的矢量化,matlab,vectorization,Matlab,Vectorization,我写了这段代码,它工作得很好,但是如果可能的话,我想通过删除两个for循环来优化它。有人知道我怎样才能做到这一点吗?非常感谢 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.0

我写了这段代码,它工作得很好,但是如果可能的话,我想通过删除两个for循环来优化它。有人知道我怎样才能做到这一点吗?非常感谢

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

因此,您希望将此代码矢量化:

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
第一步,拆除外环:

for l=1:num
    L_Mid(:,l)=sum(LMat(:,1+in_point(l):out_point(l)),2); % Using the dim input to sum
    L_Sides(:,l) = bsxfun(@minus,sum(LMat,2),L_Mid(:,l)); % Using bsxfun to subtract
end
下一步,可以通过单个操作创建L_边:

for l=1:num
    L_Mid(:,l)=sum(LMat(:,1+in_point(l):out_point(l)),2); % Using the dim input to sum
end

L_Sides = bsxfun(@minus,sum(LMat,2),L_Mid);
由于in_点(l):out_点(l)的长度是可变的,因此没有清晰的方法将其矢量化(据我所知,任何人都有一个好方法,我很想知道!),您可以保持原样,或者使用以下方法:

L_Mid2 = arrayfun(@(x) ...
    sum(LMat(:,1+in_point(x):out_point(x)),2), 1:length(in_point),'uniformoutput',false);
L_Mid2=cat(2,L_Mid2{:})

但是这不会带来性能上的好处,也不太明显,所以我不会使用这段代码。

你能将这一点最小化到一个平衡点吗?这非常优雅,休!我印象深刻!非常感谢。