Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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!
Posted
Comments
Sergey Alexandrovich Kryukov 28-Feb-12 15:47pm    
First, this is not a question.
Second, "convert" is not a correct term. Not just formally: you have two different structures, say double[,] or double[][] and double[]. What's the required mapping between them? It's neither "default" nor "assumed".
--SA

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

Something like:
C#
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[^].
 
Share this answer
 
v2
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900