如何得到这个矩阵

发布于 2024-12-29 00:47:50 字数 335 浏览 1 评论 0原文

我有这个矩阵:

 S.No.  A         B  
 1     5268020   1756  
 2     15106230  5241  
 3     24298744  9591  
 4     23197375  9129  

我想得到一个有两列 [X,Y] 的矩阵。 X 将从 S.No 中获取值。 Y 可以是 1 或 0。例如,对于 1 5268020 1756,总共应该有 5268020 (1,0),即 (X,Y) 对和1756 (1,1) 对。

我怎样才能在Octave中得到这个矩阵?

I have this matrix:

 S.No.  A         B  
 1     5268020   1756  
 2     15106230  5241  
 3     24298744  9591  
 4     23197375  9129  

I want to get a matrix which will have two columns [X,Y]. X will take values from S.No. and Y will can be either 1 or 0. For example, for 1 5268020 1756 there should be total 5268020 (1,0) i.e, (X,Y) pairs and 1756 (1,1) pairs.

How can I get this matrix in Octave ??

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

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

发布评论

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

评论(1

橘亓 2025-01-05 00:47:50

如果我正确理解你的问题,你想用重复的条目 (x,0) 和 (x,1) 填充矩阵,其中 x=1...4,其中重复由 A 列和 B 列中的值确定。给定您提供的值,这将是一个巨大的矩阵(67,896,086 行)。因此,您可以尝试这样的操作(替换下面的 m,出于说明目的,它的元素较少):

m = [1, 2, 1; 
     2, 3, 2; 
     3, 2, 1; 
     4, 2, 2];
res = [];
for k = 1:4
  res = [res ; [k*ones(m(k, 2), 1), zeros(m(k, 2), 1); 
                k*ones(m(k, 3), 1), ones(m(k, 3), 1)]];
endfor

出于

res =

   1   0
   1   0
   1   1
   2   0
   2   0
   2   0
   2   1
   2   1
   3   0
   3   0
   3   1
   4   0
   4   0
   4   1
   4   1

好奇,是否有任何理由不考虑像

1  0  n
1  1  m
2  0  p
2  1  q
...

where n, mpq 是在 A 列和 B 列中找到的值。这可能更容易处理,不是吗?

If I understand your question correctly, you want to fill a matrix with repeated entries (x,0) and (x,1), where x=1...4, where repetition is determined by values found in column A and B. Given the values you supplied that's going to be a huge matrix (67,896,086 rows). So, you could try something like this (replace m below, which has less elements for illustrative purpose):

m = [1, 2, 1; 
     2, 3, 2; 
     3, 2, 1; 
     4, 2, 2];
res = [];
for k = 1:4
  res = [res ; [k*ones(m(k, 2), 1), zeros(m(k, 2), 1); 
                k*ones(m(k, 3), 1), ones(m(k, 3), 1)]];
endfor

which yields

res =

   1   0
   1   0
   1   1
   2   0
   2   0
   2   0
   2   1
   2   1
   3   0
   3   0
   3   1
   4   0
   4   0
   4   1
   4   1

Out of curiosity, is there any reason not to consider a matrix like

1  0  n
1  1  m
2  0  p
2  1  q
...

where n, m, p, q, are values found in columns A and B. This would probably be easier to handle , no?

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