Multidimensional array column sorting - c#

assume i create this code to generate the idv-th random number. But i have a difficulty in sorting the array depends on the last column.
let say the individual size is [idv, width] = [8,6]
and i want to sort all the row with column 6th... and i want to take the 4 top list in the array after it sorted. How can i implement this case to the code??
public static void population(double[,] individual, int width, int idv, Random rnd)
{
for (int i = 0; i < idv; i++)
{
Console.Write("individual {0} :\t", i+1);
for (int j = 0; j < width; j++)
{
individual[i, j] = Math.Round(rnd.NextDouble() * 10, 2);
Console.Write("{0} ", individual[i, j]);
}
Console.WriteLine("\n");
}
}
Thank you

I suggest you using jagged arrays double[][] instead of 2d ones double[,]. Jagged array is just an array of array which you can easily sort, filter, etc. usually with a help of Linq:
double[][] individual = new double[][] {
new double[] {81, 82, 83, 84, 85, 86},
new double[] {11, 12, 13, 14, 15, 16},
new double[] {41, 42, 43, 44, 45, 46},
new double[] {31, 32, 33, 34, 35, 36},
new double[] {51, 52, 53, 54, 55, 56},
new double[] {21, 22, 23, 24, 25, 26},
new double[] {61, 62, 63, 64, 65, 66},
new double[] {71, 72, 73, 74, 75, 76},
};
double[][] fragment = individual
.OrderBy(line => line[line.GetUpperBound(0)]) // by last column
.Take(4)
.ToArray();
One more Linq to test the results:
String test = String.Join(Environment.NewLine, fragment
.Select(line => String.Join("\t", line)));
Console.Write(test);
The result is
11 12 13 14 15 16
21 22 23 24 25 26
31 32 33 34 35 36
41 42 43 44 45 46

If you use multidimensional arrays, there's no easy way to work with them using LINQ, you'll need to rely on good old for.
static void Main ()
{
// Input array
double[,] u = {
{ 1, 9, 3 },
{ 0, 3, 4 },
{ 3, 4, 5 },
{ 3, 6, 8 },
{ 3, 5, 7 },
};
// Get dimension sizes, specify column to order by
int count = u.GetLength(0), length = u.GetLength(1), orderBy = 2;
// Result array
double[,] v = new double[count, length];
// Construct a list of indices to sort by
var indices = Enumerable.Range(0, count).OrderBy(i => u[i, orderBy]).ToList();
// Copy values from input to output array, based on these indices
for (int i = 0; i < count; i++)
for (int j = 0; j < length; j++)
v[i, j] = u[indices[i], j];
PrintArray(u);
Console.WriteLine();
PrintArray(v);
}
static void PrintArray (double[,] a)
{
for (int i = 0; i < a.GetLength(0); i++) {
for (int j = 0; j < a.GetLength(1); j++)
Console.Write(a[i, j]);
Console.WriteLine();
}
}
If you need only top 4 rows, you can add Take(4) before ToList() call and adjust result array creation and copying of values into it appropriately.
Multidimenional arrays are more efficient and use less memory, so if your arrays are big enough or you need to work with them faster, you may need to write a little more code and use multidimenional arrays instead of jagged arrays which are easier to work with.

For rectangular array [,] you can copy the desired column in a single-dimensional array together with indices, sort it, and then get rows with first 4 indices.
static IEnumerable<Tuple<int, double>> GetColumn(int columnIndex, double[,] a)
{
for (int i = a.GetLowerBound(0); i <= a.GetUpperBound(0); i++)
yield return Tuple.Create(i, a[i, columnIndex]);
}
double [,] data = ...;
var column = GetColumn(4, data);
var top4Indices = column.OrderBy(v => v.Second)
.Take(4)
.Select(v => v.First);
foreach (int i in top4Indices)
for (int j = data.GetLowerBound(1); j <= data.GetUpperBound(1); j++)
data[i, j]...

Related

C# Multiply Each row of array by set of factors

I have a multidimensional array and I want to multiply each row of it by a single dimensional array containing factors to create a new multidimensional array. However, the number of rows in the multidimensional array will vary from one run to the next, so I'll need a way to loop the multiplication through each row of the multidimensional array until the last row is reached.
Here is a simple example, I have multidimensional array:
{ { 1, 2, 3, 4}, { 5, 6, 7, 8}, { 9, 10, 11, 12} }
and I want to multiply each row by:
{0.5, 0.4, 0, 0.8}
to get:
{ { 1*0.5, 2*0.4, 3*0, 4*0.8}, { 5*0.5, 6*0.4, 7*0, 8*0.8}, { 9*0.5, 10*0.4, 11*0, 12*0.8} }
I've tried to use .Length within a for loop using the example code.
double [] factors = {0.5, 0.4, 0, 0.8};
int [,] myarray = new int [3,4] {
{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9, 10, 11, 12}
};
double [] output = new double[myarray.Length];
for(int i = 0; i < myarray.Length; ++i)
output[i] = myarray[i] * factors;
I'm getting syntax errors that extension method 'Length' cannot be applied to type int, and also indexing with [] cannot be applied to type int.
I'd suggest a ragged array. Multidimensional arrays exist, but are poorly supported and almost nobody uses them. If you use a jagged array, you can be more concise. More importantly, you can be more intentional about it:
double[][] myArray = getMeSomeData();
double[] factors = getMeSomeRowFactors();
foreach (var row in myArray)
{
MultiplyRow(row, factors)
}
. . .
void MultiplyRow( double[] row, double[] factors )
{
for ( int col = 0 ; col < row.length ; ++col )
{
row[col] = row[col] * factors[col];
}
}
Or even better: use Linq:
double[][] myArray = getMeSomeData();
double[] factors = getMeSomeRowFactors();
myArray = myArray
.Select( row => row.Select( (cell, i) => cell * factors[i] ).ToArray() )
.ToArray()
;
Or even [arguably] better;
double[][] myArray = getMeSomeData();
double[] factors = getMeSomeRowFactors();
myArray = myArray
.Select( Multiply(factors) )
.ToArray()
;
. . .
Func<double,int,double[]> Multiply( double[] factors )
{
return (cell, i) => cell * factors[i];
}
I am not sure if you find it useful, but you can achieve it by using MathNet.Numerics:
PM> Install-Package MathNet.Numerics
using MathNet.Numerics.LinearAlgebra.Double;
using System.Linq;
...
var myarray = new double[3, 4]
{
{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9, 10, 11, 12}
};
var factors = new double[4] { 0.5, 0.4, 0, 0.8 };
var matrix1 = Matrix.Build
.DenseOfArray(myarray);
var matrix2 = Matrix.Build
.DenseOfRows(Enumerable.Repeat(factors, matrix1.RowCount));
var res = matrix1.PointwiseMultiply(matrix2).ToArray();
You can get the length of each dimension by using the method GetLength:
double[,] output = new double[myarray.GetLength(0), myarray.GetLength(1)];
for (int i = 0; i < myarray.GetLength(0); i++)
{
for (int j = 0; j < myarray.GetLength(1); j++)
{
output[i, j] = myarray[i, j] * factors[j];
}
}
In case your array is huge, you can speed up the calculation by using all processors/cores of your PC:
Parallel.For(0, myarray.GetLength(0), i =>
{
for (int j = 0; j < myarray.GetLength(1); j++)
{
output[i, j] = myarray[i, j] * factors[j];
}
});

Last number in an array becomes first

I know that's easy, but I don't understand how I should do it.
1 23 29 18 43 20 5
to
5 1 23 29 18 43 20
I think we should use for-loop:
for (int i = 0; i < numbers.Count - 1; i++)
{
}
but I don't know what to do in it. Something like numbers[i] = numbers[i - 1] but it isn't working. I think there are some if checks which I miss.
The most straightforward way that comes to mind is a reverse loop.
int[] numbers = { 1, 23, 29, 18, 43, 20, 5};
int lastVal = numbers[numbers.Length - 1];
for (int i = numbers.Length -1; i > 0; i--)
numbers[i] = numbers[i-1];
numbers[0] = lastVal;
Just looping from the end (after saving the last value) and moving "up" the values, finally replacing the first value with the last
Here's a oneliner:
var numbers = new[] {1, 23, 29, 18, 43, 20, 5};
numbers = new[] {numbers.Last()}.Concat(numbers.Take(numbers.Length - 1)).ToArray();
This creates a new array containing the last element, then concatenates it with the original array excluding the last element.
What you want to do is make another array of the same size as the original one, then assign the last element and loop through the array up to the previous to last element.
Something like this:
int[] original = {1, 23, 29, 18, 43, 20, 5};
int[] altered = new int[original.length];
altered[0] = original[original.length - 1];
for (int i = 1; i < original.length - 1; i++)
{
altered[i] = original[i - 1];
}
You can perform a left rotation to the table by six positions on your case and create the requested new table
int[] myArray = new int[] { 1, 23, 29, 18, 43, 20, 5 };
var newArray = LeftRotationByD(myArray, 6);
and your function for the rotation would be:
private static int[] LeftRotationByD(int[] a, int k)
{
int[] b = new int[a.Length];
int index;
int length = a.Length;
int place;
for (int i = 0; i < length; i++)
{
index = i - k;
place = length + index;
if (index >= 0) b[index] = a[i];
else b[place] = a[i];
}
return b;
}
You can use this method to shift right:
void ShiftRight(int[] array, int count)
{
var clone = (int[])array.Clone();
for (var i = 0; i < array.Length; i++)
array[(i + count) % array.Length] = clone[i];
}
And use it this way:
var a = new int[] { 1, 2, 3, 4 };
ShiftRight(a, 1);

C# split 1D array into 2D array for every Nth value

I have a 1D array of ints:
int[] array = { 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32,33, 34,40,41,42,43, 44};
I would like to divide this 1D array into a 2D array of 4 rows and 5 columns, where the first 5 values goes into row 1, the next 5 in row 2 and so on. The final result should look like this:
array2D:
[[10, 11, 12, 13, 14]
[20, 21, 22, 23, 24]
[30, 31, 32, 33, 34]
[40, 41, 42, 43, 44]]
In reality the array will be much longer(maybe 100+ rows), but the number of columns is 5 and number of rows is dividable by 5. I have simplified for example. This is what I have tried so far:
int[] array = { 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32,33, 34,40,41,42,43, 44};
int[,] array2D = new int[(array.Length/5),5];
int count_0 = 1;
int count_1 = 1;
int count_2 = 1;
int count_3 = 1;
int count_4 = 1;
for (int i = 0; i < array.Length; i++)
{
if (i < 5)
{
// works for the first 5 values:
array2D[0, i] = array[i];
}
// I tried this approach for the rest where I try to say that if Im at index 5,
//and every 5th element from here, append to it to index[i,0] of array2D and so on.
// This do not work, and is what I need help with.
else if (i >= 5 && i % 5 == 0)
{
array2D[count_0, 0] = array[i];
count_0++;
}
else if (i >= 6 && i % 5 == 0)
{
array2D[count_1, 1] = array[i];
count_1++;
}
else if (i >= 7 && i % 5 == 0)
{
array2D[count_2, 2] = array[i];
count_2++;
}
else if (i >= 8 && i % 5 == 0)
{
array2D[count_3, 3] = array[i];
count_3++;
}
else if (i >= 9 && i % 5 == 0)
{
array2D[count_4, 4] = array[i];
count_4++;
}
}
Of course for this example I could just say if > 5 && <10 {append to array2D} and if > 10 && <15 {append to array2D} and so on, but I want something that works for a large array of hundreds of values. If someone has a smarter way to do this please let me know.
Thank you for any help!
You can just calculate the indexes:
for(int i=0;i<array.Length;i++)
array2D[i/5, i%5] = array[i];
You can calculate the indexes easily using simple division. The row is calculated by the dividing i with the wanted column numbers. The column is simply the reminder of that division.
using System;
public class Program
{
public static T[,] SplitInto2DArray<T>(T[] array, int rows, int columns) {
T[,] result = new T[rows, columns];
for (int i = 0; i < array.Length; i++) {
result[i / columns, i % columns] = array[i];
}
return result;
}
public static void PrintArray<T>(T[,] array) {
for (int i = 0; i < array.GetLength(0); i++) {
for (int j = 0; j < array.GetLength(1); j++) {
Console.Write(array[i, j] + " ");
}
Console.WriteLine();
}
}
public static void Main()
{
int[] array = { 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32, 33, 34, 40, 41, 42, 43, 44};
int[,] splittedArray = SplitInto2DArray(array, 4, 5);
PrintArray(splittedArray);
}
}
You can accomplish with LINQ using Enumerable.GroupBy.
array.GroupBy(x => x / 5)
.Select(x => x.ToArray())
.ToArray()

Get array slices in length order

I have the following code to get a list of all possible slice start/end indexes, in order of descending slice length. I then iterate over the list and use it to slice up an array, breaking when I get a slice that I am looking for. I do this because I am looking for the largest slice that matches other parameters and I am looking to short cut past the other possible slices once I've found one.
I would prefer a couple of nested for loops to check the slice and move on, instead of having to get every possible range and sort them first, since the array can be up to a billion or so items large. I can't for the life of me figure out how to do it, or even how to phrase the question to search for it. Any help is appreciated.
byte[] data1 = { 54, 87, 23, 87, 45, 67, 7, 85, 65, 65, 3, 4, 55, 76, 65, 64, 5, 6, 4, 54, 45, 6, 4 };
List<Tuple<int, int>> ranges = new List<Tuple<int, int>>();
for (int i1 = 0; i1 < data1.Count(); i1++)
{
for (int i2 = i1 + 1; i2 < data1.Count(); i2++)
{
ranges.Add(new Tuple<int, int>(i1, i2));
}
}
ranges = ranges.OrderByDescending(x => x.Item2 - x.Item1).ToList();
If I understand the question correctly, you are looking for the largest subarray of an array (which you are calling a "slice"…maybe a term from some other programming language?) that meets some specific conditions. In your question, you are unspecific about the conditions themselves, so I assume that part is not important.
It seems what you are having difficulty with is arranging your code so that you necessarily inspect the longest subarrays first.
If all of that is correct, then you just need to arrange your loops differently. Currently, you are picking a starting index and then finding all subarrays that start at that index. Instead, since you want the longest subarrays to be inspect first, you should pick the length of the subarray, starting with the longest possible length, and select all subarrays that can be that long.
For example:
for (int i = data1.Length; i > 0; i--)
{
for (int j = 0; j < data1.Length - i + 1; j++)
{
// inspect subarray starting at index j, having length i
}
}
You may enumerate the slices directly by a couple of nested loops:
bool found = false;
for (int sliceLen = data1.Length; !found && sliceLen > 0; sliceLen--)
for (int sliceStart = 0; !found && sliceStart + sliceLen <= data1.Length; sliceStart++)
if (found = (
data1[sliceStart] == data1[sliceStart + sliceLen - 1] // check your condition here
))
Console.WriteLine($"Found: {sliceStart}:{sliceLen}");
Demo: https://ideone.com/lZRNJm
I figured it out. Here's what I needed, to iterate over each array slice in reverse length order.
byte[] data1 = { 54, 87, 23, 87, 45, 67, 7, 85, 65, 65, 3, 4, 55, 76, 65, 64, 5, 6, 4, 54, 45, 6, 4 };
for (int length = data1.Count() - 1; length > 0; length--)
{
int numberOfSlicesForLength = data1.Count() - length;
for (int start = 0; start < numberOfSlicesForLength; start++)
{
byte[] sliceValues = data1.Skip(start).Take(length);
}
}

Sum of all numbers in a multi-dimensional array

I'm currently learning C# and running into some trouble with multi dimension array.
I can't seem to figure out how to find the sum of all the elements in the array. E.g
int[,] array = { { 52, 76, 65 }, { 98, 87, 93 }, { 43, 77, 62 }, { 72, 73, 74 } };
So the sum should be:
52 + 76 + 65 + 98 + ...
I have tried to use the for loop but that just give me the sum of each array in dimension 1. E.g
private int[,] array = { { 52, 76, 33}, { 98, 87, 93 }, { 43, 77, 62 }, { 72, 73, 74 } };
public void ArraySum()
{
double sum;
for (int i = 0; i < array.GetLength(0); i++)
{
sum = 0;
for (int j = 0; j < array.GetLength(1); j++)
sum += array[i, j];
Console.WriteLine("The sums are for the array {0} is {1}: ", i, sum);
}
}
Any ideas?
The other answer pointed out what's wrong with your code. Here's a short alternative to your loops using LINQ:
int[,] array = { { 52, 76, 65 }, { 98, 87, 93 }, { 43, 77, 62 }, { 72, 73, 74 } };
int sum = array.Cast<int>().Sum();
The Cast is used to convert the multidimensional array to an IEnumerable<int> which then allows you to use the Sum extension method.
Since you're just learning and playing around anyway, you could use a List<int<int>> to store your values instead of a 2-dimensional array, like this:
var array = new List<List<int>>
{
new List<int> {52, 76, 65},
new List<int> {98, 87, 93},
new List<int> {43, 77, 62},
new List<int> {72, 73, 74}
};
Then you could easily use LINQ to get your sum:
var sum = array.SelectMany(x => x).Sum();
If you haven't heard of it yet, you can learn more about LINQ here.
Basically, SelectMany() flattens your multiple lists into one list, and then Sum() just adds them all up.
Simply move the line sum = 0; out of the first loop:
private int[,] array = { { 52, 76, 33}, { 98, 87, 93 }, { 43, 77, 62 }, { 72, 73, 74 } };
public void ArraySum()
{
int sum = 0;
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
sum += array[i, j];
}
Console.WriteLine("The sum for the whole array is {0}: ", sum);
}
In your code the sum is reset to 0 for each sub-array.
Edit
Since the array contains int values, consider to declare the sum variable as int instead of double.
If all you want is the sum of the entire array, then
private static int[,] array = { { 52, 76, 33}, { 98, 87, 93 }, { 43, 77, 62 }, { 72, 73, 74 } };
public static void Main()
{
double sum = 0;
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
sum += array[i, j];
}
Console.WriteLine("The sum for the array is {0}: ", sum);
}

Categories