MATLAB 中超大矩阵的高效乘法

发布于 2024-10-07 04:48:59 字数 486 浏览 6 评论 0 原文

我没有足够的内存来简单地创建对角 D-by-D 矩阵,因为 D 很大。我不断收到“内存不足”错误。

我没有在第一个乘法中执行 M x D x D 运算,而是执行 M x D 运算,但我的代码仍然需要很长时间才能运行。

有人能找到更有效的方法来执行乘法A'*B*A吗?这是我到目前为止所做的尝试:

D=20000
M=25

A = floor(rand(D,M)*10);
B = floor(rand(1,D)*10);

for i=1:D
    for j=1:M
        result(i,j) = A(i,j) * B(1,j);
    end
end    

manual = result * A';
auto = A*diag(B)*A';
isequal(manual,auto)

alt text

I don't have enough memory to simply create a diagonal D-by-D matrix, since D is large. I keep getting an 'out of memory' error.

Instead of performing M x D x D operations in the first multiplication, I do M x D operations, but still my code takes ages to run.

Can anybody find a more effective way to perform the multiplication A'*B*A? Here's what I've attempted so far:

D=20000
M=25

A = floor(rand(D,M)*10);
B = floor(rand(1,D)*10);

for i=1:D
    for j=1:M
        result(i,j) = A(i,j) * B(1,j);
    end
end    

manual = result * A';
auto = A*diag(B)*A';
isequal(manual,auto)

alt text

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

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

发布评论

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

评论(3

月下客 2024-10-14 04:48:59

应该解决您的问题的一种选择是使用稀疏矩阵。下面是一个示例:

D = 20000;
M = 25;
A = floor(rand(D,M).*10);    %# A D-by-M matrix
diagB = rand(1,D).*10;       %# Main diagonal of B
B = sparse(1:D,1:D,diagB);   %# A sparse D-by-D diagonal matrix
result = (A.'*B)*A;         %'# An M-by-M result

另一种选择是沿 B 的主对角线复制 D 元素,以使用函数 REPMAT,然后使用 元素-明智乘法A.'

B = repmat(diagB,M,1);   %# Replicate diagB to create an M-by-D matrix
result = (A.'.*B)*A;    %'# An M-by-M result

还有一个选择是使用函数BSXFUN

result = bsxfun(@times,A.',diagB)*A;  %'# An M-by-M result

One option that should solve your problem is using sparse matrices. Here's an example:

D = 20000;
M = 25;
A = floor(rand(D,M).*10);    %# A D-by-M matrix
diagB = rand(1,D).*10;       %# Main diagonal of B
B = sparse(1:D,1:D,diagB);   %# A sparse D-by-D diagonal matrix
result = (A.'*B)*A;         %'# An M-by-M result

Another option would be to replicate the D elements along the main diagonal of B to create an M-by-D matrix using the function REPMAT, then use element-wise multiplication with A.':

B = repmat(diagB,M,1);   %# Replicate diagB to create an M-by-D matrix
result = (A.'.*B)*A;    %'# An M-by-M result

And yet another option would be to use the function BSXFUN:

result = bsxfun(@times,A.',diagB)*A;  %'# An M-by-M result
梦过后 2024-10-14 04:48:59

也许我在这里有点傻,但是你不能将你的 DxD 矩阵转换为 DxM 矩阵(带有给定向量的 M 个副本),然后 .* 最后两个矩阵而不是将它们相乘(然后,当然,通常将第一个乘以找到的产品数量)?

Maybe I'm having a bit of a brainfart here, but can't you turn your DxD matrix into a DxM matrix (with M copies of the vector you're given) and then .* the last two matrices rather than multiply them (and then, of course, normally multiply the first with the found product quantity)?

我的黑色迷你裙 2024-10-14 04:48:59
  1. 您出现“内存不足”的情况,因为 MATLAB 找不到足够大的内存块来容纳整个矩阵。有不同的技术可以避免此错误,所述 在 MATLAB 文档中

  2. 在 MATLAB 中,大多数情况下您显然不需要显式循环编程,因为您可以使用运算符 *。如果使用显式循环完成,存在一种如何加速矩阵乘法的技术,这里是 C# 示例。它很好地了解了如何将(可能很大的)矩阵拆分为更小的矩阵。要在 MATLAB 中包含这些较小的矩阵,可以使用元胞矩阵。更有可能的是,系统找到足够的 RAM 来容纳两个较小的子矩阵,然后容纳生成的大矩阵。

  1. You are getting "out of memory" because MATLAB can not find a chunk of memory large enough to accommodate the entire matrix. There are different techniques to avoid this error described in MATLAB documentation.

  2. In MATLAB you obviously do not need programming explicit loops in most cases because you can use operator *. There exists a technique how to speed up matrix multiplication if it is done with explicit loops, here is an example in C#. It has a good idea how (potentially large) matrix can be split into smaller matrices. To contain these smaller matrices in MATLAB you can use cell matrix. It is much more probably that system finds enough RAM to accommodate two smaller sub-matrices then the resulting large matrix.

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