在 MATLAB 中标准化矩阵的快速技术

发布于 2024-10-08 23:22:21 字数 389 浏览 1 评论 0原文

我想在 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 秒。那么,有没有更好的解决方案呢?

I want to normalise each column of a matrix in Matlab. I have tried two implementations:

Option 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

Option B:

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

However, these options take too much time for my data, e.g. 3-4 seconds on a 5000x53 matrix. Thus, is there any better solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

千纸鹤 2024-10-15 23:22:22

注意:此代码适用于 Octave 和 MATLAB R2016b 或更高版本。

function X_norm = normalizeMatrix(X)      
      mu = mean(X); %mean    
      sigma = std(X); %standard deviation   
      X_norm = (X - mu)./sigma;    
end

Note: This code works in Octave and MATLAB versions R2016b or higher.

function X_norm = normalizeMatrix(X)      
      mu = mean(X); %mean    
      sigma = std(X); %standard deviation   
      X_norm = (X - mu)./sigma;    
end
愛放△進行李 2024-10-15 23:22:22

如何使用

normc(X)

它来标准化矩阵 X 的列。不过,您需要在安装中包含神经网络工具箱。

How about using

normc(X)

that would normalize the matrix X columnwise. You need to include the Neural Network Toolbox in your install though.

阿楠 2024-10-15 23:22:22

这个怎么样?

A = [7, 2, 6; 3、8、4]; % 2x3 矩阵

Asum = sum(A); % 对列求和

Anorm = A./Asum(ones(size(A, 1), 1), :); % 标准化列

How about this?

A = [7, 2, 6; 3, 8, 4]; % a 2x3 matrix

Asum = sum(A); % sum the columns

Anorm = A./Asum(ones(size(A, 1), 1), :); % normalise the columns

初懵 2024-10-15 23:22:21

使用 bsxfun 而不是循环。这可能会快一点;但是,它也可能使用更多内存(这可能是您的情况的问题;如果您进行分页,一切都会非常慢)。

要使用平均值和标准差进行标准化,您可以写

mn = mean(x);
sd = std(x);
sd(sd==0) = 1;

xn = bsxfun(@minus,x,mn);
xn = bsxfun(@rdivide,xn,sd);

Use bsxfun instead of the loop. This may be a bit faster; however, it may also use more memory (which may be an issue in your case; if you're paging, everything'll be really slow).

To normalize with mean and std, you'd write

mn = mean(x);
sd = std(x);
sd(sd==0) = 1;

xn = bsxfun(@minus,x,mn);
xn = bsxfun(@rdivide,xn,sd);
没有心的人 2024-10-15 23:22:21

请记住,在 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

Remember, in MATLAB, vectorizing = speed.

If A is an M x N matrix,

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
枯寂 2024-10-15 23:22:21

Xmx n 矩阵,并且您希望按列标准化。

以下 matlab 代码执行此操作

XMean = repmat(mean(X),m,1);
XStd = repmat(std(X),m,1);
X_norm = (X - XMean)./(XStd);

这里解释了元素明智的 ./ 运算符: http: //www.mathworks.in/help/matlab/ref/arithmeticoperators.html

注意: 正如前面提到的,这只是一个更快的解决方案,并且执行与循环矩阵相同的任务。这个内置函数的底层实现使其工作速度更快

Let X be a m x n matrix and you want to normalize column wise.

The following matlab code does it

XMean = repmat(mean(X),m,1);
XStd = repmat(std(X),m,1);
X_norm = (X - XMean)./(XStd);

The element wise ./ operator is explained here: http://www.mathworks.in/help/matlab/ref/arithmeticoperators.html

Note: As op mentioned, this is simply a faster solution and performs the same task as looping through the matrix. The underlying implementation of this inbuilt function makes it work faster

娜些时光,永不杰束 2024-10-15 23:22:21

注意:我没有提供全新的答案,但我正在比较建议的答案。

选项 A:使用 bsxfun()

function xn = normalizeBsxfun(x)

    mn = mean(x);
    sd = std(x);
    sd(sd==0) = eps;

    xn = bsxfun(@minus,x,mn);
    xn = bsxfun(@rdivide,xn,sd);

end

选项 B:使用 for 循环

function xn = normalizeLoop(x)

    xn = zeros(size(x));

    for ii=1:size(x,2)
        xaux = x(:,ii);
        xn(:,ii) = (xaux - mean(xaux))./mean(xaux);
    end

end

我们比较不同矩阵大小的两种实现:

expList = 2:0.5:5;
for ii=1:numel(expList)
    expNum = round(10^expList(ii));
    x = rand(expNum,expNum); 
    tic;
    xn = normalizeBsxfun(x);
    ts(ii) = toc; 
    tic;
    xn = normalizeLoop(x);
    tl(ii) = toc; 
end

figure;
hold on;
plot(round(10.^expList),ts,'b');
plot(round(10.^expList),tl,'r');
legend('bsxfun','loop');
set(gca,'YScale','log') 

结果表明,对于小矩阵,bsxfun 更快。但是,对于更高的维度,这种差异是可以忽略的,因为在其他 帖子

在此处输入图像描述

x 轴是矩阵元素的平方根数,y 轴是计算时间几秒钟内。

Note: I am not providing a freshly new answer, but I am comparing the proposed answers.

Option A: Using bsxfun()

function xn = normalizeBsxfun(x)

    mn = mean(x);
    sd = std(x);
    sd(sd==0) = eps;

    xn = bsxfun(@minus,x,mn);
    xn = bsxfun(@rdivide,xn,sd);

end

Option B: Using a for-loop

function xn = normalizeLoop(x)

    xn = zeros(size(x));

    for ii=1:size(x,2)
        xaux = x(:,ii);
        xn(:,ii) = (xaux - mean(xaux))./mean(xaux);
    end

end

We compare both implementations for different matrix sizes:

expList = 2:0.5:5;
for ii=1:numel(expList)
    expNum = round(10^expList(ii));
    x = rand(expNum,expNum); 
    tic;
    xn = normalizeBsxfun(x);
    ts(ii) = toc; 
    tic;
    xn = normalizeLoop(x);
    tl(ii) = toc; 
end

figure;
hold on;
plot(round(10.^expList),ts,'b');
plot(round(10.^expList),tl,'r');
legend('bsxfun','loop');
set(gca,'YScale','log') 

The results show that for small matrices, the bsxfun is faster. But, the difference is neglect able for higher dimensions, as it was also found in other post.

enter image description here

The x-axis is the squared root number of matrix elements, while the y-axis is the computation time in seconds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文