创建指标矩阵

发布于 2024-11-09 21:53:12 字数 382 浏览 0 评论 0原文

对于大小为 nx 1 的向量 V,我想创建大小为 nx Max(V) 的二元指示矩阵 M 使得M的行条目在相应的列索引中具有1,否则为0

例如:如果V

V = [ 3
      2
      1
      4]

指标矩阵应该是

M= [ 0 0 1 0
     0 1 0 0
     1 0 0 0
     0 0 0 1]

For a vector V of size n x 1, I would like to create binary indicator matrix M of the size n x Max(V) such that the row entries of M have 1 in the corresponding columns index, 0 otherwise.

For eg: If V is

V = [ 3
      2
      1
      4]

The indicator matrix should be

M= [ 0 0 1 0
     0 1 0 0
     1 0 0 0
     0 0 0 1]

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

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

发布评论

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

评论(6

宣告ˉ结束 2024-11-16 21:53:12

像这样的指标矩阵的问题是,如果使它稀疏的话会更好。无论如何,你几乎总是会用它进行矩阵乘法,因此要使该乘法成为有效的乘法。

n = 4;
V = [3;2;1;4];
M = sparse(V,1:n,1,n,n);
M =
   (3,1)        1
   (2,2)        1
   (1,3)        1
   (4,4)        1

如果您坚持 M 是一个满矩阵,那么事后通过使用 full 使其变得如此简单。

full(M)
ans =
     0     0     1     0
     0     1     0     0
     1     0     0     0
     0     0     0     1

了解如何使用稀疏矩阵。这样做你将会受益匪浅。诚然,对于 4x4 矩阵,稀疏不会有太大增益。但示例案例从来都不是您真正的问题。假设 n 确实是 2000?

n = 2000;
V = randperm(n);
M = sparse(V,1:n,1,n,n);
FM = full(M);

whos FM M
  Name         Size                 Bytes  Class     Attributes

  FM        2000x2000            32000000  double              
  M         2000x2000               48008  double    sparse    

稀疏矩阵不仅仅在内存使用方面有所提高。比较单个矩阵乘法所需的时间。

A = magic(2000);

tic,B = A*M;toc
Elapsed time is 0.012803 seconds.

tic,B = A*FM;toc
Elapsed time is 0.560671 seconds.

The thing about an indicator matrix like this, is it is better if you make it sparse. You will almost always be doing a matrix multiply with it anyway, so make that multiply an efficient one.

n = 4;
V = [3;2;1;4];
M = sparse(V,1:n,1,n,n);
M =
   (3,1)        1
   (2,2)        1
   (1,3)        1
   (4,4)        1

If you insist on M being a full matrix, then making it so is simple after the fact, by use of full.

full(M)
ans =
     0     0     1     0
     0     1     0     0
     1     0     0     0
     0     0     0     1

Learn how to use sparse matrices. You will gain greatly from doing so. Admittedly, for a 4x4 matrix, sparse will not gain by much. But the example cases are never your true problem. Suppose that n was really 2000?

n = 2000;
V = randperm(n);
M = sparse(V,1:n,1,n,n);
FM = full(M);

whos FM M
  Name         Size                 Bytes  Class     Attributes

  FM        2000x2000            32000000  double              
  M         2000x2000               48008  double    sparse    

Sparse matrices do not gain only in terms of memory used. Compare the time required for a single matrix multiply.

A = magic(2000);

tic,B = A*M;toc
Elapsed time is 0.012803 seconds.

tic,B = A*FM;toc
Elapsed time is 0.560671 seconds.
惟欲睡 2024-11-16 21:53:12

执行此操作的快速方法 - 如果您不需要稀疏矩阵 - 是创建一个大小至少为 max(v) 的单位矩阵,然后通过从 v 中提取索引来创建指示矩阵:

m = max(V);
I = eye(m);
V = I(V, :);

a quick way to do this - if you do not require sparse matrix - is to create an identity matrix, of size at least the max(v), then to create your indicator matrix by extracting indexes from v:

m = max(V);
I = eye(m);
V = I(V, :);
喵星人汪星人 2024-11-16 21:53:12

为了记忆起见,您希望创建稀疏索引矩阵。这很简单:

vSize = size(V);
Index = sparse(vSize(1),max(V));
for i = 1:vSize(1)
    Index(i, v(i)) = 1;
end

我自己用过这个,享受吧:)

You would like to create the Index matrix to be sparse for memory sake. It is as easy as:

vSize = size(V);
Index = sparse(vSize(1),max(V));
for i = 1:vSize(1)
    Index(i, v(i)) = 1;
end

I've used this myself, enjoy :)

草莓味的萝莉 2024-11-16 21:53:12

您只需将 V 中的列索引与行索引组合起来即可创建 线性索引,然后用它来填充M(初始化为零):

M = zeros(numel(V), max(V));
M((1:numel(V))+(V.'-1).*numel(V)) = 1;

You can simply combine the column index in V with a row index to create a linear index, then use that to fill M (initialized to zeroes):

M = zeros(numel(V), max(V));
M((1:numel(V))+(V.'-1).*numel(V)) = 1;
眼泪也成诗 2024-11-16 21:53:12

这是另一种方法,类似于 sparse 但使用 accumarray

V = [3; 2; 1; 4];
M = accumarray([(1:numel(V)).' V], 1);

Here's another approach, similar to sparse but with accumarray:

V = [3; 2; 1; 4];
M = accumarray([(1:numel(V)).' V], 1);
笨死的猪 2024-11-16 21:53:12
M=sparse(V,1:size(V,1),1)';

将生成一个稀疏矩阵,您可以将其作为完整版本用于计算。
您可以使用 full(M) 来“膨胀”M 以实际存储零。

M=sparse(V,1:size(V,1),1)';

will produce a sparse matrix that you can use in calculations as a full version.
You could use full(M) to "inflate" M to actually store zeros.

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