我需要对以下matlab代码进行矢量化,现在对于更大的矩阵A和多重向量m来说速度很慢:
function cA = condenseM(A,m)
% condensation of full matrix A (MxM) to type matrix (MxT)
% m is vector of type multiplicities [m1, ...,mT], where
% M = sum(m)
% M,T
M = sum(m);
T = length(m);
% "0" + last item index over all types
blockTypeIndx = [0 cumsum(m)];
% condensed matrix generation
cA = zeros(M,T);
for i = 1:T
if m(i) > 1
cA(:,i) = max(A(:,blockTypeIndx(i)+1:blockTypeIndx(i+1)),[],2);
else
cA(:,i) = A(:,blockTypeIndx(i)+1:blockTypeIndx(i+1));
end
end
end
对于矩阵A(6x6),这是一个简单且通用的示例案例:
A =
0 3 3 | 1 1 | 6
4 0 0 | 0 0 | 2
0 0 0 | 5 0 | 0
0 1 1 | 4 4 | 1
2 0 0 | 0 0 | 5
0 0 0 | 0 3 | 0
m1=3 m2 = 2 m3=1
T = 3的多重向量m:
m =
3 2 1
浓缩矩阵cA看起来像:
cA = condenseM(A,m)
cA =
3 1 6
4 0 2
0 5 0
1 4 1
2 0 5
0 3 0
答案 0 :(得分:0)
这是使用cellfun进行矢量化的可能代码,但在我的机器上它比你循环慢:
<ItemGroup>
<Reference Include="System.Management.Automation" />
</ItemGroup>