如何使用重复行创建矩阵n * n [英] how create matrix n*n with duplicate Rows

查看:85
本文介绍了如何使用重复行创建矩阵n * n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iam为Matrix 1024 * 10编写此代码

i想填充矩阵0和1随机

条件没有重复(重复)行

此示例不正确

1

iam write this code for Matrix 1024*10
i want fill matrix with 0 and 1 with Random
With the Condition that there is no repeat(duplicate) row
This example is incorrect
1

0 1 0 1  1 0 1 0 0 1 
0 1 1 0  1 0 0 0 0 0
0 1 0 1  1 0 1 0 0 1 
.
.
.







int d = 0 ,g=0;
           string s = "";
           string[] arrayList1=new string[1024] ;
           int n = 1024, m = 10;
           System.Random random = new System.Random();
           double[,] array = new double[n, m];
           for (int i = 0; i < 1024; ++i)
           {
               for (int j = 0; j < 10; ++j)
               {
                  array[i, j] = random.Next(0, 2) ;
                  s =s+  array[i, j].ToString();
                  Console.Write("  " + array[i, j].ToString() );
                 }



           }
           Console.WriteLine(g);
           Console.ReadLine();

           Console.ReadKey();

       }

推荐答案

实用的方法:)



如果我真的以正确的方式理解你的问题,你想制作一个1024行的矩阵,其中10行的行应该只包含'1'或'0 '。



这意味着二进制表示的每个模式都在你的1024行中,因为2 ^ 10 = 1024.



如果以上是正确的,只需创建一个二进制represantation为0 ... 1023的矩阵作为列,并且ramdomly交换行大约1024次。



注意:它是MxN矩阵然后是NxN。
A pragmatic Approach :)

If I really understand your question in the right way you like to make a Matrix with 1024 rows filled with lines having 10 columns which should contain only '1' or '0'.

That means every pattern for binary representation will be there in your 1024 rows, because 2^10 = 1024.

If the above is right, simply create a Matrix with binary represantation of 0...1023 as columns and ramdomly exchange rows about 1024 times.

N.B: It is more MxN Matrix then a NxN.


我不会一点一点地做;我一次生成32位。你需要生成10x32整数(1024 = 32x32),然后将它们插入阵列。



算法:

0)实例化HashSet< int>和一个队列< int>

1)获取Random.Next()

2)如果新的int不在Hashset中,则将其添加到Hashset和Queue

3)如果队列中的int数少于320,则转到1

4)每行迭代10行乘32英寸

5)出队下一个int

6)获取值的基数2表示形式System.Convert.ToString(intval,2).PadLeft(32,'0')

7)追加(见StringBuilder)字符串到当前行





A这种技术的局限在于每个int的最重要位总是为零。如果这是一个问题,你可以一次做十六位。或者尝试NextBytes:

http://msdn.microsoft.com/en-us/library/vstudio/system.random.nextbytes(v = vs.100).aspx [ ^ ]





我还想指出,比较的东西越大,差异就越小。因此,如果您生成10个完整的1024位值,那么任何两个值可能只相差一位(尽管概率很小)。

通过生成320个32位值,我相信结果为1024保证比特值相差至少32位。如果您使用64个16位值,那么您可以预期至少64个差异。

如果差异的数量很重要,那么您可能需要计算Levenshtein距离并比较某个阈值而不是一个简单的等于/不等于支票。



PS我看到我把阵列的形状颠倒了,没什么大不了的。生成0到1023之间的所有值,然后以随机顺序将它们放入数组中:



I wouldn't do it bit-by-bit; I'd generate 32 bits at a time. You'll need to generate 10x32 integers (1024=32x32), then just plug them into the array.

Algorithm:
0) Instantiate a HashSet<int> and a Queue<int>
1) Get Random.Next()
2) If the new int is not in the Hashset, then add it to the Hashset and Queue
3) If fewer than 320 ints in the Queue, goto 1
4) Iterate 10 rows by 32 ints per row
5) Dequeue the next int
6) Get the base-2 representation of the value System.Convert.ToString ( intval , 2 ).PadLeft ( 32 , '0' )
7) Append (see StringBuilder) the string to the current row


A limitation of this technique is that the most-significant bit of each int will always be zero. If that is a concern, you could do sixteen bits at a time instead. Or try NextBytes:
http://msdn.microsoft.com/en-us/library/vstudio/system.random.nextbytes(v=vs.100).aspx[^]


I would also like to point out that the larger the things compared, the smaller the difference is required. So if you produce 10 full 1024-bit values, then any two values may differ by only one bit (although that has a small probability).
By generating 320 32-bit values I believe the resultant 1024-bit values are guaranteed to differ by at least 32 bits. And if you use 64 16-bit values, then you can expect at least 64 differences.
If the number of differences matters, then you may need to calculate the Levenshtein Distance and compare to some threshold rather than a simple equal/not-equal check.

P.S. I see that I got the shape of the array reversed, no big deal. Generate all the values from 0 to 1023 and then put them into the array in random order:

int[] a = new int [ 1024 ] ;
for ( int i = 0 ; i < a.Length ; i++ ) a [ i ] = i ;
System.Collections.Generic.List<string> s =
  new System.Collections.Generic.List<string> ( a.Length ) ;
foreach ( int i in a.OrderBy ( x => System.Guid.NewGuid() ) )
  s.Add ( System.Convert.ToString ( i , 2 ).PadLeft ( 10 , '0' ) ) ;


的想法是:随机创建一个raw然后检查这行是否已经在矩阵中找到了。如果它已经找到,请再次尝试随机生成,重复直到你得到独特的原始。



但是如何更好地使用它。首先,定义表示raw的类(一维数组可以是其成员之一)并为其定义比较方法。它将需要覆盖方法 Equals ,这也需要覆盖 GetHashCode 。此外,定义自定义运算符'=='和'!='(它需要定义两者)可能很有用。

请参阅:

http://msdn.microsoft.com/en-us/library/bsc2ak47(v=vs .110).aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.object.gethashcode(v = vs.110).aspx [ ^ ]。



为什么?它允许您将此类的实例存储为 System.Collections.Generic.Dictionary 的键或:

http://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs。 110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/bb359438%28v=vs.110%29.aspx [ ^ ]。



为什么?这些类强制密钥的唯一性,并为检查唯一性提供O(1)时间复杂度。您可以使用行添加键类的元素,同时将行添加到矩阵中。这个冗余数据将帮助您检查下一行的唯一性。



参见:

http://en.wikipedia.org/wiki/Big_O_notation [ ^ ],

http:// en.wikipedia.org/wiki/Time_complexity [ ^ ]。



-SA
The idea is: create a raw randomly and then check up if this row is already found in the matrix. If it is already found, try with random generation again, repeat until you get unique raw.

But how to use that better. First, define class representing the raw (a 1-dimensional array could be one of its members) and define the comparison methods for it. It will require to override the methods Equals, which will also requires overriding GetHashCode. Also, it might be useful to define custom operators '==' and '!=' (it requires to define both).
Please see:
http://msdn.microsoft.com/en-us/library/bsc2ak47(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.object.gethashcode(v=vs.110).aspx[^].

Why? It will allow you to store instances of this class as the keys of System.Collections.Generic.Dictionary or :
http://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/bb359438%28v=vs.110%29.aspx[^].

Why? These classes force uniqueness of the key and provide O(1) time complexity for the check of uniqueness. You add an element of your key class with the row, along with adding the row to a matrix. This redundant data will help you to check up uniqueness of the next row.

See also:
http://en.wikipedia.org/wiki/Big_O_notation[^],
http://en.wikipedia.org/wiki/Time_complexity[^].

—SA


这篇关于如何使用重复行创建矩阵n * n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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