Matlab不能优化以下内容吗? [英] Doesn't Matlab optimize the following?

查看:61
本文介绍了Matlab不能优化以下内容吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很长的向量1xr v和一个非常长的向量w 1xs,还有一个矩阵A rxs,它稀疏(但尺寸很大).

I have a very long vector 1xr v, and a very long vector w 1xs, and a matrix A rxs, which is sparse (but very big in dimensions).

我期望Matlab对以下内容进行优化,这样我就不会在内存方面遇到麻烦:

I was expecting the following to be optimized by Matlab so I won't run into trouble with memory:

 A./(v'*w)

,但是Matlab似乎实际上正在尝试生成完整的v'*w矩阵,因为我遇到了内存不足的问题.有办法克服吗?请注意,无需计算所有v'*w,因为A的许多值都是0.

but it seems like Matlab is actually trying to generate the full v'*w matrix, because I am running into out of memory issue. Is there a way to overcome this? Note that there is no need to calculate all v'*w because many values of A are 0.

如果可能的话,执行此操作的一种方法是执行A(find(A))./(v'*w)(find(A));

If that were possible, one way to do it would be to do A(find(A))./(v'*w)(find(A));

但是您必须先计算矩阵并将其放入变量中,然后才能选择矩阵的子集(在这种情况下为v'*w).

but you can't select a subset of a matrix (v'*w in this case) without first calculating it and putting it in a variable.

推荐答案

  • 您可以使用bsxfun.这给出了与A./(v'*w)相同的结果,而没有生成矩阵v.'*w:

    • You could use bsxfun. This gives the same result as A./(v'*w) without generating the matrix v.'*w:

      bsxfun(@rdivide, bsxfun(@rdivide, A, v'), w)
      

    • 另一种可能性:如果只需要非零值,请使用:

    • Another possibility: if you only want the nonzero values, use:

      [ii jj Anz] = find(A);
      Anz./v(ii)'./w(jj).'
      

      这将再次提供与您的A(find(A))./(v'*w)(find(A))相对应的列向量,而不会生成v.'*w.如果需要稀疏矩阵A./(v'*w)(而不是其列向量的非零值),请使用sparse(ii,jj,Anz./v(ii)'./w(jj).').

      This gives a column vector corresponding to your A(find(A))./(v'*w)(find(A)), again without generating v.'*w. If you need the sparse matrix A./(v'*w) (instead if the column vector of its nonzero values), use sparse(ii,jj,Anz./v(ii)'./w(jj).').

      这篇关于Matlab不能优化以下内容吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆