在 MATLAB 中标准化矩阵的快速技术
我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
注意:此代码适用于 Octave 和 MATLAB R2016b 或更高版本。
Note: This code works in Octave and MATLAB versions R2016b or higher.
如何使用
它来标准化矩阵 X 的列。不过,您需要在安装中包含神经网络工具箱。
How about using
that would normalize the matrix X columnwise. You need to include the Neural Network Toolbox in your install though.
这个怎么样?
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
使用 bsxfun 而不是循环。这可能会快一点;但是,它也可能使用更多内存(这可能是您的情况的问题;如果您进行分页,一切都会非常慢)。
要使用平均值和标准差进行标准化,您可以写
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
请记住,在 MATLAB 中,矢量化 = 速度。
如果 A 是 M x N 矩阵,
Remember, in MATLAB, vectorizing = speed.
If
A
is an M x N matrix,令
X
为mx n
矩阵,并且您希望按列标准化。以下 matlab 代码执行此操作
这里解释了元素明智的 ./ 运算符: http: //www.mathworks.in/help/matlab/ref/arithmeticoperators.html
注意: 正如前面提到的,这只是一个更快的解决方案,并且执行与循环矩阵相同的任务。这个内置函数的底层实现使其工作速度更快
Let
X
be am x n
matrix and you want to normalize column wise.The following matlab code does it
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
注意:我没有提供全新的答案,但我正在比较建议的答案。
选项 A:使用 bsxfun()
选项 B:使用 for 循环
我们比较不同矩阵大小的两种实现:
结果表明,对于小矩阵,bsxfun 更快。但是,对于更高的维度,这种差异是可以忽略的,因为在其他 帖子。
x 轴是矩阵元素的平方根数,y 轴是计算时间几秒钟内。
Note: I am not providing a freshly new answer, but I am comparing the proposed answers.
Option A: Using
bsxfun()
Option B: Using a for-loop
We compare both implementations for different matrix sizes:
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.The x-axis is the squared root number of matrix elements, while the y-axis is the computation time in seconds.