C# - Getting List<byte> reference index - linq

I have a List>
List<List<Byte>> bytes = new List<List<Byte>>()
{
new List<Byte> {1, 1, 2, 3, 4}, // index 0 is original
new List<Byte> {0, 0, 2, 4, 1},
new List<Byte> {1, 2, 2, 1, 1},
new List<Byte> {1, 0, 2, 2, 2}
};
and the first List is original. Then I sort my List, and I have to find my original List index.
bytes:
[0] = {0, 0, 2, 4, 1}
[1] = {1, 0, 2, 2, 2}
[2] = {1, 1, 2, 3, 4} // here is that index
[3] = {1, 2, 2, 1, 1}
One guy suggested me to use reference on first not sorted List then sort List and use ReferenceEqual, but this doesn't work for me. Am I doing something wrong by setting reference or it doesn't work on List? How could I get the index of original in sorted array? P.S I'm sorting with OrderBy and IComparer.
This is how I try to make reference:
List<byte> reference = new List<byte>(bytes[0]);

This is how I try to make reference:
List<byte> reference = new List<byte>(bytes[0]);
That's not the right way to make a reference, because you make a copy by calling a constructor of List<byte>. The copy of bytes[0] is not present in bytes, so you wouldn't be able to find it by checking reference equality.
This is how you should do it:
List<byte> reference = bytes[0];
Now reference references the list that is at position zero before sorting, so you should be able to find its index by using reference equality.

Related

I have to generate 10000 numbers which are the result of 12 random numbers subtracted by 0.5 and then added together

I'm stuck here, and can't generate a do loop that repeats this operation 10k times and result in a list or array
{((RandomVariate[TruncatedDistribution[{0, 1}, NormalDistribution[]],
12])) - 0.5}
Do does things but it doesn't generate output. E.g.
Do[{1, 2, 3}, 2]
- no output -
Have it add to a list though...
alist = {};
Do[alist = Join[alist, {1, 2, 3}], 2]
alist
{1, 2, 3, 1, 2, 3}

Sort a matrix according to one row in Mathematica

I'm fairly new to Mathematica and I've come across a probably stupid problem.
I have a matrix {{1,1,1,2,2,2,2,2},{-1,0,1,-2,-1,0,1,2}} and I would like to sort the second row but also that the positions of the elements of the first row are sorted at the same time.
Thus, the array would become {{2,1,2,1,2,1,2,2},{-2,-1,-1,0,0,1,1,2}}. I hope is it clear. Do you know how I could proceed?
Thanks in advance.
Let
list = {{1, 1, 1, 2, 2, 2, 2, 2}, {-1, 0, 1, -2, -1, 0, 1, 2}}
then
{list[[1]][[Ordering[list[[2]]]]], Sort[list[[2]]]}
gives the output you are looking for.
For future reference, you might want to think about posting Mathematica questions at https://mathematica.stackexchange.com.
Another approach:
SortBy[Transpose[data],Last]//Transpose
{{2, 1, 2, 1, 2, 1, 2, 2}, {-2, -1, -1, 0, 0, 1, 1, 2}}

How to calculate a matrix formed by vector in Mathematica

I need to obtain a matrix vvT formed by a column vector v. i.e. the column vector v matrix times its transpose.
I found Mathematica doesn't support column vector. Please help.
Does this do what you want?
v = List /# Range#5;
vT = Transpose[v];
vvT = v.vT;
v // MatrixForm
vT // MatrixForm
vvT // MatrixForm
To get {1, 2, 3, 4, 5} into {{1}, {2}, {3}, {4}, {5}} you can use any of:
List /# {1, 2, 3, 4, 5}
{ {1, 2, 3, 4, 5} }\[Transpose]
Partition[{1, 2, 3, 4, 5}, 1]
You may find one of these more convenient than the others. Usually on long lists you will find Partition to be the fastest.
Also, your specific operation can be done in different ways:
x = {1, 2, 3, 4, 5};
Outer[Times, x, x]
Syntactically shortest:

Maximize function in Mathematica which counts elements

I reduced a debugging problem in Mathematica 8 to something similar to the following code:
f = Function[x,
list = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5};
Count[list, x]
];
f[4]
Maximize{f[x], x, Integers]
Output:
4
{0, {x->0}}
So, while the maximum o function f is obtained when x equals 4 (as confirmed in the first output line), why does Maximize return x->0 (output line 2)?
The reason for this behavior can be easily found using Trace. What happens is that your function is evaluated inside Maximize with still symbolic x, and since your list does not contain symbol x, results in zero. Effectively, you call Maximize[0,x,Integers], hence the result. One thing you can do is to protect the function from immediate evaluation by using pattern-defined function with a restrictive pattern, like this for example:
Clear[ff];
ff[x_?IntegerQ] :=
With[{list = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5}}, Count[list, x]]
It appears that Maximize can not easily deal with it however, but NMaximize can:
In[73]:= NMaximize[{ff[x], Element[x, Integers]}, x]
Out[73]= {4., {x -> 4}}
But, generally, either of the Maximize family functions seem not quite appropriate for the job. You may be better off by explicitly computing the maximum, for example like this:
In[78]:= list = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5};
Extract[#, Position[#, Max[#], 1, 1] &[#[[All, 2]]]] &[Tally[list]]
Out[79]= {{4, 4}}
HTH
Try this:
list = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5};
First#Sort[Tally[list], #1[[2]] > #2[[2]] &]
Output:
{4, 4}

how to import a file into mathematica and reference a column by header name

I have a TSV file with many columns like so;
genename X1 X100 X103 X105 X115 X117 X120 X122 X123
Gene20728 0.415049 0.517868 0.820183 0.578081 0.30997 0.395181
I would like to import it into Mathematica, and then extract and sort a column.
i.e., I want to extract column ["X117"] and sort it, and output the sorted list.
table = Import["file.csv", "Table"];
x117 = Drop[table[[All, 7]], 1];
sorted = Sort[x117];
I do not think there is a built in method of achieving the smart structure you seem to be asking for.
Below is the what I think is the most straight forward implementation out of the various possible methods.
stringdata = "h1\th2\n1\t2\n3\t4\n5"
h1 h2
1 2
5 4
3
Clear[ImportColumnsByName];
ImportColumnsByName[filename_] :=
Module[{data, headings, columns, struc},
data = ImportString[filename, "TSV"];
headings = data[[1]];
columns = Transpose[PadRight[data[[2 ;; -1]]]];
MapThread[(struc[#1] = #2) &, {headings, columns}];
struc
]
Clear[test];
test = ImportColumnsByName[stringdata];
test["h1"]
test["h2"]
Sort[test["h1"]]
outputs:
{1, 3, 5}
{2, 4, 0}
{1, 3, 5}
Building on ragfield's solution, this is a more dynamic method, however every call to this structure makes a call to Position and Part.
Clear[ImportColumnsByName];
ImportColumnsByName[filename_] := Module[{data, temp},
data = PadRight#ImportString[filename, "Table"];
temp[heading_] :=
Rest[data[[All, Position[data[[1]], heading][[1, 1]]]]];
temp
]
Clear[test];
test = ImportColumnsByName[stringdata];
test["h1"]
test["h2"]
Sort[test["h1"]]
outputs:
{1, 3, 5}
{2, 4, 0}
{1, 3, 5}
Starting from ragfield's code:
table = Import["file.csv", "Table"];
colname = "X117"
x117 = Drop[table[[All, Position[tb[[1, All]], colname]//Flatten]],
1]//Flatten;
sorted = Sort[x117];
For processing Excel files from various sites I do variations on this:
data = {{"h1", "h2"}, {1, 2}, {3, 4}, {5, ""}};
find[x_String] := Cases[Transpose[data], {x, __}]
In[]=find["h1"]
Out[]={{"h1", 1, 3, 5}}
If it is ragged sort of data you can usually pad it readily enough to make it suitable for transposing. Additionally some of my sources are lazy with formatting, sometimes headers change case, sometimes there is an empty row before the header, and so on:
find2[x_String,data_List] :=
Cases[Transpose[data], {___,
y_String /;
StringMatchQ[StringTrim[y], x, IgnoreCase -> True], __}]
In[]=find2["H1",data]
Out[]={{"h1", 1, 3, 5}}
data2 = {{"", ""}, {"H1 ", "h2"}, {1, 2}, {3, 4}, {5, ""}};
In[]=find2["h1",data2]
Out[]={{,"H1 ", 1, 3, 5}}

Resources