Octave:表示标准基向量的矩阵

发布于 2024-12-17 00:09:03 字数 144 浏览 0 评论 0原文

假设我有一个矩阵,其中每一行都是一个标准基向量,即每一行恰好包含一个 1,其他列为 0。

是否有一种方便的方法来创建这样的矩阵(即给定一个位置向量,其中每行)?

另外,有没有一种方法可以表示这样一个矩阵,以便可以在八度音程中更有效地进行乘法?

Suppose I have a matrix such that each row is a standard basis vector, i.e. each row contains exactly one 1, the other columns being 0.

Is there a convenient way to create such a matrix (i.e. given a vector of positions of where the ones are in each row)?

Also, is there a way I should represent such a matrix so that multiplications with it can be done more efficiently in octave?

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

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

发布评论

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

评论(1

倒数 2024-12-24 00:09:04

假设您想要一个 3x3 矩阵,其中的元素分别位于第 3、1 和 2 列中:

> pos = [3,1,2];
> x = eye(3)(pos,:);

将为您提供一个包含 9 个元素的矩阵,其中大部分为零,元素位于所需的位置。您可以使用稀疏表示来节省内存:sparse_x =稀疏(x);。但在我的机器上进行的以下测试表明自然形式的倍增速度更快:

> N = 10000;
> s = rand(N,N);
> x = eye(N)(randperm(N),:);
> sx = sparse(x);
> t = cputime(); ss = s*x; cputime()-t
ans = 0.41124
> t = cputime(); ss2 = s*sx; cputime()-t
ans = 1.0313

这是 Core i7 上的 Octave 3.4,YMMV。

看看 whos ,Octave 似乎正在用 x 做一些聪明的事情:

> whos
Variables in the current scope:

  Attr Name        Size                     Bytes  Class
  ==== ====        ====                     =====  ===== 
       N           1x1                          8  double
       s       10000x10000              800000000  double
       ss      10000x10000              800000000  double
       ss2     10000x10000              800000000  double
       sx      10000x10000                 160004  double
       x       10000x10000                  40000  double  <---SMALLER THAN s!

如果它知道 x 是特殊的,也许它已经在利用加速乘法。

Suppose you want a 3x3 matrix with the ones in columns 3, 1, and 2 respectively:

> pos = [3,1,2];
> x = eye(3)(pos,:);

will give you a matrix with 9 elements, most zero, with the ones in the desired places. You can save memory by using a sparse representation: sparse_x = sparse(x);. But the following test on my machine implies that the natural form multiplies faster:

> N = 10000;
> s = rand(N,N);
> x = eye(N)(randperm(N),:);
> sx = sparse(x);
> t = cputime(); ss = s*x; cputime()-t
ans = 0.41124
> t = cputime(); ss2 = s*sx; cputime()-t
ans = 1.0313

This was Octave 3.4 on a Core i7, YMMV.

Looking at whos it appears that Octave is doing something clever with x:

> whos
Variables in the current scope:

  Attr Name        Size                     Bytes  Class
  ==== ====        ====                     =====  ===== 
       N           1x1                          8  double
       s       10000x10000              800000000  double
       ss      10000x10000              800000000  double
       ss2     10000x10000              800000000  double
       sx      10000x10000                 160004  double
       x       10000x10000                  40000  double  <---SMALLER THAN s!

If it knows x is special, maybe it's already taking advantage of speedups in the multiplication.

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