This question already has answers here:
Rotate - Transposing a List<List<string>> using LINQ C#
(7 answers)
Closed 5 years ago.
I have a List of a List (a matrix, which is sent via web.api HTTP POST from matlab).
List<List<decimal>> mylist;
The size of the matrix is nxm, how can I swap those lists? i.e.
mylist[i][j] --> mylist[j][i]
In Matlab the operation would be mylist' or in mathematical context (transposing)
mylist^T
You could use Linq to achieve that without for loop like this:
var swapedList =
mylist
.SelectMany((l, i) => l.Select((d, j) => new { i, j, d }))
.GroupBy(l=>l.j)
.Select(l=>l.Select(ll=>ll.d).ToList());
.ToList();
I hope to be helpful for you :)
Related
This question already has answers here:
Random number generator with no duplicates
(12 answers)
Generating random numbers without repeating.C# [duplicate]
(11 answers)
Closed 1 year ago.
I am trying to work out how to make a random number generator that outputs 4 integers going from 0 to 9 without any repeats.
Would like some help please
i have just started coding in c# but cant find any answers to my issue
You need to remove the int from the checking if it has already repeated.
so:
if (val1 == val2)
{
val2 = rnd.Next(1,11);
}
and not:
if (val1 == val2)
{
int val2 = rnd.Next(1,11);
}
The latter will declare a new variable "val2" which exists only inside the scope of the "if" block, instead of updating the existing "val2" variable as you intended. This is called "shadowing".
For "quick and dirty" solution you can use LINQ. For example something like this will randomly select 4 numbers in range from 0 to 9:
var random = new Random();
var numbers = Enumerable.Range(0, 10)
.OrderBy(_ => random.Next())
.Take(4)
.ToList();
In case you need something more prescise and faster then you can implement Fisher–Yates shuffle for example.
This question already has answers here:
C# Sort List Based on Another List
(7 answers)
C# Sort list by IDs, but without creating a new list
(2 answers)
Sorting two separate Lists in the same order?
(4 answers)
Sort two Lists<T> together as one?
(7 answers)
Identically sorting two lists
(4 answers)
Closed 1 year ago.
I need to sort one list (string) according to another list values (int) from the smallest to the highest int.
Possibility to have several times the same string.
For example:
List<string> list1= new List<string>() { "abc", "xyzz", "abc", "fgh", "abc" };
List<int> list2 = new List<int>() { 8, 15, 4, 2, 22 };
output:
list1 {"fgh","abc", "abc", "xyzz", "abc"}
This line should probably work but I still miss something:
list1 = list1.OrderBy(d => list2."i miss something here".ToList());
This question already has answers here:
Variables in a loop
(9 answers)
Loop through object variables with different number on the name [duplicate]
(5 answers)
Iteration with variable name [duplicate]
(1 answer)
Closed 2 years ago.
I am trying to use a for loop to iterate through a series of variable names each ending in a number from 1 to 10. I have seen a few other answers to this question but have been unable to make any work for my specific situation. My code is as follows:
string cat2Pos0 = cat2[0];
int numOfPos0 = cat2.Where(x => x.Equals(cat2Pos0)).Count();
List<int> indexOfPos0 = new List<int>();
bool check = cat2.Contains(cat2Pos0);
int index = 0;
if (check == true)
{
for (int i = 0; i < numOfPos0; i++)
{
index = cat2.FindIndex(x => x == cat2Pos0);
indexOfPos0.Add(cat2.IndexOf(cat2Pos0));
}
}
else if (cat2Pos0 == "-")
{
numOfPos0 = 17;
}
I need to loop through 10 variables names cat1 - cat10. In the code: whenever there is the phrase "cat" I need to be able to adjust it depending on a for loop e.g. cat1 or cat5:
string cat3pos0 = cat3[0];
or:
index = cat3.FindIndex(x => x == cat3Pos0);
Unfortuantely, I am unable to simply write out each variation individually as that would use up almost 3700 lines of code and I was hoping that there would be a better way of achieveing this.
Many thanks, all help is greatly appreciated,
Josh
See here how to use reflection for this. (something like this.GetType().GetField("cat" + i.ToString());.)
But I would really suggest changing your variables to one array of 10 variables. So cat will be an array of arrays (since your cat's seem to be arrays).
This question already has answers here:
How can I sort a List<T> by multiple T.attributes?
(3 answers)
Closed 4 years ago.
I was wondering if it is possible to combine my sorting code into one ?
I sort my array firstly by enum, then by an id number from lowest to highest.
So my current code looks like this:
_entities = _entities.OrderBy(a => a.Priority).ToArray(); //sort by enum
_entities = _entities.OrderBy(a => a.PriorityID).ToArray(); //then sort by id
So the enum has:
High, Medium,Low
And IDs are just integers.
So the end result should be like:
High¬
0 , 1 ,2
Medium¬
0, 1, 2
Low¬
0, 1, 2
I don't know the syntax to combine these, or if this is as simple as it gets? But it does seem a bit inefficient to be sorting by each property one by one.
Whats the correct way to do ths?
You can use ThenBy:
_entities = _entities.OrderBy(a => a.Priority).ThenBy(a => a.PriorityID).ToArray();
This question already has answers here:
How do I get the index of the highest value in an array using LINQ?
(10 answers)
Closed 7 years ago.
Say I have a Point:
public class Point
{
double X, Y;
}
I want to get the index of the element inside List<Point> that satisfy a condition, for example, with the maximum Point.X value inside List<Point>.
How would I do that with a LINQ expression?
You can do using this Select() overlaod which takes index of item as well:
var result = Points.Select((Point,Index)=> new { Index,Point})
.OrderByDescending(x=>x.Point.X).First().Index;