如何在C#中将矩阵转换为向量并对向量进行排序 [英] How to convert matrix to vector in C#, and sort vector

查看:84
本文介绍了如何在C#中将矩阵转换为向量并对向量进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个40x60的矩阵,其中包含像素值.我想将其转换为一维矢量,然后对这些值进行排序.

在C ++中,我相信我会使用std:vector库,但不确定在C#中使用哪种库或方法.

我是C#的初学者,非常感谢任何建议/提示.谢谢!

解决方案

创建一个新列表,遍历矩阵并将所有值添加到列表中,然后调用list.Sort()

像这样的东西:

列表< int> pixelValues =  List< int>();

/*  遍历矩阵,并使用pixelValues.add(value)*/添加每个值

pixelValues.Sort(); 



最好我可以使用提供的信息来提出.您想要用存储在矩阵中的任何类型替换int,但我不确定矩阵的结构是什么样,但是我猜您知道如何迭代它.

如果您需要除默认比较之外的其他功能,则可以使用排序"的重载,请参见 MSDN页面以获取列表 [ ^ ].


假设您具有元素类型T(实际上是intdouble或其他类型).

矩阵可以表示为T[,].该大小将在初始化时固定.对于固定维矩阵,这是最合适的.

对于锯齿状,可以使用T[][](数组数组),而对于完全支持的即时插入或删除,可以使用System.Collections.Generic.List<System.Collections.Generic.List<T>>.我认为您不需要此任务.

对于矢量,只需要数组T[],因为在计算时,可以从源矩阵的尺寸中得知尺寸.如果由于某种原因需要向量,则为System.Collections.Generic.List<T>>.

祝你好运,
—SA


I have a 40x60 matrix containing pixel values. I want to convert that to a 1-D vector and then sort those values.

In C++ I believe I would use the std:vector library, but I am unsure what library or method to use in C#.

I am a beginner to C# and would greatly appreciate any advice/tips. Thank you!

解决方案

Create a new list, iterate through your matrix and add all the values to the list, then call list.Sort()

Something like:

List<int> pixelValues = new List<int>();

/* iterate through your matrix and add each value with pixelValues.add(value) */

pixelValues.Sort();



Best I can come up with using the provided information. You''ll want to replace int with whatever type you''re storing in your matrix, and I''m not sure what the matrix''s structure looks like but I''m guessing you know how to iterate through it.

If you need something other than the default comparison there are overloads of Sort available, see the MSDN page for List[^].


Let''s assume you have an element type T (actually int, double or something).

A matrix can be represented as T[,]. The size would be fixed at the moment of initialization. This is the most adequate for a fixed-dimension matrix.

For jagged, you would use T[][] (array of arrays), and, for fully supported insertion or deletion on the fly, it could be System.Collections.Generic.List<System.Collections.Generic.List<T>>. I don''t think you need it for this task.

For a vector, you need just the array T[], because at by the moment of calculation, the dimension is known from the dimensions of the source matrix. If by some reason you need a vector, this would be System.Collections.Generic.List<T>>.

Good luck,
—SA


这篇关于如何在C#中将矩阵转换为向量并对向量进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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