我想在Matlab中规范化矩阵的每一列。我尝试了两种实现:

选项A:

mx=max(x);
mn=min(x);
mmd=mx-mn;
for i=1:size(x,1)
    xn(i,:)=((x(i,:)-mn+(mmd==0))./(mmd+(mmd==0)*2))*2-1;
end

选项B:
mn=mean(x);
sdx=std(x);
for i=1:size(x,1)
    xn(i,:)=(x(i,:)-mn)./(sdx+(sdx==0));
end

但是,这些选项占用我的数据太多时间,例如在5000x53矩阵上需要3-4秒。因此,有没有更好的解决方案?

最佳答案

请记住,在MATLAB中,矢量化=速度。

如果A是M x N矩阵,

A = rand(m,n);
minA = repmat(min(A), [size(A, 1), 1]);
normA = max(A) - min(A);               % this is a vector
normA = repmat(normA, [length(normA) 1]);  % this makes it a matrix
                                       % of the same size as A
normalizedA = (A - minA)./normA;  % your normalized matrix

10-08 04:14