Converting a 2D Array of 2D Arrays into a single 2D Array - c#

So I have an object called, for lack of a better word, MatricesMatrix which is a Matrix of Matrices (everything is stored as a double[,]). I want to strip all of the values from the inner matrices into one big Matrix. Here is what I have so far:
public Matrix ConvertToMatrix()
{
//Figure out how big the return matrix should be
int totalRows = this.TotalRows();
int totalColumns = this.TotalColumns();
Matrix returnMatrix = new Matrix(totalRows, totalColumns);
List<object> rowElementsList = new List<object>();
//"outer" index means an index of the MatricesMatrix
//"inner" index means an index of a Matrix within the Matrices Matrix
//outer row loop
for (int outerRowIndex = 0; outerRowIndex < NumberOfRows; outerRowIndex++)
{
//outer column loop
for (int outerColumnIndex = 0; outerColumnIndex < NumberOfColumns; outerColumnIndex++)
{
Matrix currentMatrix = GetElement(outerRowIndex, outerColumnIndex);
object element = null;
//inner row loop
for (int innerRowIndex = 0; innerRowIndex < currentMatrix.NumberOfRows; innerRowIndex++)
{
//inner column loop
for (int innerColumnIndex = 0; innerColumnIndex < currentMatrix.NumberOfColumns; innerColumnIndex++)
{
element = currentMatrix.GetElement(innerRowIndex, innerColumnIndex);
}
}
returnMatrix.SetElement(outerRowIndex, outerColumnIndex, (double)element);
}
}
return returnMatrix;
}
Note that I have determined programmatically the total number of rows and columns the returnMatrix needs to have.
Here are some more guidelines and output cases:
Each element of the big matrix should be in the same position relative to the other elements of the big matrix that came from the Matrix inside of MatricesMatrix that the element came from.
Each "matrix" (no longer in matrix form) inside of the big matrix should be in the same position relative to the other matrices inside of the big matrix as it was inside of the MatricesMatrix (with no overlapping, and 0's in any spaces left empty).
CASE 1
Given this input: a MatricesMatrix(2,2) with [0,0] = (2x2 matrix), [0,1] = (2x3 matrix), [1,0] = (2x2 matrix), and [1,1] = (2x3 matrix). That is,
Output must be:
CASE 2
Given this input: a MatricesMatrix(2,2) with [0,0] = (1x1 matrix), [0,1] = (3x3 matrix), [1,0] = (2x2 matrix), and [1,1] = (4x4 matrix). That is,
Output should be something like:
Any assistance would be greatly appreciated!
UPDATE:
Here is a unit test for Case 1 that should pass:
[TestMethod]
public void MatricesMatrix_ConvertToMatrixTest()
{
Matrix m1 = new Matrix(2);
Matrix m2 = new Matrix(2, 3);
Matrix m3 = new Matrix(2);
Matrix m4 = new Matrix(2, 3);
double[] m1Row1 = { 1, 1 };
double[] m1Row2 = { 1, 1 };
double[] m2Row1 = { 2, 2, 2 };
double[] m2Row2 = { 2, 2, 2 };
double[] m3Row1 = { 3, 3 };
double[] m3Row2 = { 3, 3 };
double[] m4Row1 = { 4, 4, 4 };
double[] m4Row2 = { 4, 4, 4 };
m1.SetRowOfMatrix(0, m1Row1);
m1.SetRowOfMatrix(1, m1Row2);
m2.SetRowOfMatrix(0, m2Row1);
m2.SetRowOfMatrix(1, m2Row2);
m3.SetRowOfMatrix(0, m3Row1);
m3.SetRowOfMatrix(1, m3Row2);
m4.SetRowOfMatrix(0, m4Row1);
m4.SetRowOfMatrix(1, m4Row2);
MatricesMatrix testMatricesMatrix = new MatricesMatrix(2, 2);
testMatricesMatrix.SetElement(0, 0, m1);
testMatricesMatrix.SetElement(0, 1, m2);
testMatricesMatrix.SetElement(1, 0, m3);
testMatricesMatrix.SetElement(1, 1, m4);
Matrix expectedResult = new Matrix(4, 5);
double[] expectedRow1 = { 1, 1, 2, 2, 2 };
double[] expectedRow2 = { 1, 1, 2, 2, 2 };
double[] expectedRow3 = { 3, 3, 4, 4, 4 };
double[] expectedRow4 = { 3, 3, 4, 4, 4 };
expectedResult.SetRowOfMatrix(0, expectedRow1);
expectedResult.SetRowOfMatrix(1, expectedRow2);
expectedResult.SetRowOfMatrix(2, expectedRow3);
expectedResult.SetRowOfMatrix(3, expectedRow4);
Matrix actualResult = testMatricesMatrix.ConvertToMatrix();
(actualResult == expectedResult).Should().BeTrue();
}

I started with a simple Matrix class to hold the double[,]s. Nothing too fancy, just a simple array-of-arrays with a row and column count and array accessor.
class Matrix<T>
{
public int Rows { get; private set; }
public int Cols { get; private set; }
private T[,] mat;
public Matrix(int rowCount, int colCount)
{
Rows = rowCount;
Cols = colCount;
mat = new T[Rows, Cols];
}
public T this[int r, int c]
{
get { return mat[r, c]; }
set { mat[r, c] = value; }
}
}
Your second case looks more difficult (and like a better test of correctness) than the first, so I set up a metamatrix to match that.
public static Matrix<double[,]> BuildMetaMatrix()
{
Matrix<double[,]> m = new Matrix<double[,]>(2, 2);
m[0, 0] = new double[,]
{
{ 1 }
};
m[0, 1] = new double[,]
{
{ 3, 3, 3 },
{ 3, 3, 3 },
{ 3, 3, 3 }
};
m[1, 0] = new double[,]
{
{ 2, 2 },
{ 2, 2 }
};
m[1, 1] = new double[,]
{
{4, 4, 4, 4},
{4, 4, 4, 4},
{4, 4, 4, 4},
{4, 4, 4, 4}
};
return m;
}
For convenience, I made a Place function that puts one matrix into another one at the given location.
static void Place(double[,] src, double[,] dest, int destR, int destC)
{
for (int row = 0; row < src.GetLength(ROW_DIM); row++)
{
for (int col = 0; col < src.GetLength(COL_DIM); col++)
{
dest[row + destR, col + destC] = src[row, col];
}
}
}
The magic numbers fed into GetLength() were just asking for mistakes, so I defined some constants for them (ROW_DIM = 0 and COL_DIM = 1). I decided to handle the padding by figuring out how wide a column is and how tall a row is and skipping any extra elements after Place()ing the sub-matrix in. A GetRowHeight() and GetColWidth() method figure out the values.
public static int GetRowHeight(Matrix<double[,]> m, int row)
{
int maxSeen = 0;
for (int col = 0; col < m.Cols; col++)
{
if (m[row, col].GetLength(ROW_DIM) > maxSeen)
{
maxSeen = m[row, col].GetLength(ROW_DIM);
}
}
return maxSeen;
}
public static int GetColWidth(Matrix<double[,]> m, int col)
{
int maxSeen = 0;
for (int row = 0; row < m.Rows; row++)
{
if (m[row, col].GetLength(COL_DIM) > maxSeen)
{
maxSeen = m[row, col].GetLength(COL_DIM);
}
}
return maxSeen;
}
A Flatten() function loops through all the sub-matrices, Place()ing them at the appropriate row and column in a new matrix. It updates the next row and column after each Place() using the GetRowHeight() and GetColWidth() functions.
Matrix<double> Flatten(Matrix<Matrix<double>> src)
{
// (7, 6) == (this.TotalRows(), this.TotalColumns())
// from your code.
Matrix<double> dest = new Matrix<double>(7, 6);
int nextRow = 0;
int nextCol = 0;
for (int row = 0; row < src.Rows; row++)
{
for (int col = 0; col < src.Rows; col++)
{
dest.Place(src[row, col], nextRow, nextCol);
nextCol += GetColWidth(src, col);
}
nextRow += GetRowHeight(src, row);
nextCol = 0;
}
return dest;
}
A little glue to test it out...
static void Main(string[] args)
{
Matrix<double[,]> src = BuildMetaMatrix();
double[,] dest = Flatten(src);
Print(dest);
Console.ReadLine();
}
static void Print(double[,] matrix)
{
for (int row = 0; row < matrix.GetLength(ROW_DIM); row++)
{
for (int col = 0; col < matrix.GetLength(COL_DIM); col++)
{
Console.Write(matrix[row, col] + "\t");
}
Console.Write("\n");
}
}
...and you get an output just like your second case with all the oddly fitting matrices and 0s in the empty places.*
1 0 3 3 3 0
0 0 3 3 3 0
0 0 3 3 3 0
2 2 4 4 4 4
2 2 4 4 4 4
0 0 4 4 4 4
0 0 4 4 4 4
*The destination matrix gets its values initialized to default(double), which happens to be 0 (the value you wanted). If you need something other than default(double) for the empty places, you can probably get them by iterating over the new matrix and writing the new default value everywhere before Flatten()ing the metamatrix.
(Thanks to Jeff Mercado for pointing out that multidimensional arrays' GetLength() method can be used to find their dimensions.)

I think it would be beneficial to you to break up the solution into the quadrants you are trying to fill. This will all be under the assumption that we will only be combining 4 matrices in this 2x2 configuration. The same strategies explined here can be applied to other dimensions of matrices to be combined.
So given 4 matrices A, B, C and D, we will try to build a resulting matrix in this arrangement:
+---+---+
| A | B |
+---+---+
| C | D |
+---+---+
Before we can start, we will need to figure out the dimensions of the final result. This should hopefully make sense. We'll have a top half, bottom half, left half and right half.
rows_top = max(rows_A, rows_B)
rows_bottom = max(rows_C, rows_D)
rows_result = rows_top + rows_bottom
cols_left = max(cols_A, cols_C)
cols_right = max(cols_B, cols_D)
cols_result = cols_left + cols_right
Then we will want to consider which regions of the result matrix we want to copy each of the 4 matrices. Considering the origin at the top-left, everything on the right half will be shifted over by the size of the left half, everything on the bottom half will be shifted over by the size of the top half. The offsets for each of the matrices would be:
offset_A = (0, 0)
offset_B = (0, cols_left)
offset_C = (rows_top, 0)
offset_D = (rows_top, cols_left)
Now with all this information, we can start building up the result matrix. Just copy over the values from each matrix to the result, with the offsets applied.
So in code, I would do this:
// I'm just going to use plain 2D arrays here
public T[,] Combine<T>(T[,] a, T[,] b, T[,] c, T[,] d)
{
// get the total rows
var rows_top = Math.Max(a.GetLength(0), b.GetLength(0));
var rows_bottom = Math.Max(c.GetLength(0), d.GetLength(0));
var rows_result = rows_top + rows_bottom;
// get the total columns
var cols_left = Math.Max(a.GetLength(1), c.GetLength(1));
var cols_right = Math.Max(b.GetLength(1), d.GetLength(1));
var cols_result = cols_left + cols_right;
// get the offsets
var offset_a = Tuple.Create(0, 0);
var offset_b = Tuple.Create(0, cols_left);
var offset_c = Tuple.Create(rows_top, 0);
var offset_d = Tuple.Create(rows_top, cols_left);
// fill 'er up
var result = new T[rows_result, cols_result];
Fill(result, a, offset_a);
Fill(result, b, offset_b);
Fill(result, c, offset_c);
Fill(result, d, offset_d);
return result;
}
public void Fill<T>(T[,] result, T[,] source, Tuple<int, int> offset)
{
for (var i = 0; i < source.GetLength(0); i++)
for (var j = 0; j < source.GetLength(1); j++)
result[offset.Item1 + i, offset.Item2 + j] = source[i, j];
}
Then to demonstrate the result in terms of case 2:
const string A = "A", B = "B", C = "C", D = "D";
var a = new string[1,1]
{
{ A },
};
var b = new string[3, 3]
{
{ B, B, B },
{ B, B, B },
{ B, B, B },
};
var c = new string[2, 2]
{
{ C, C },
{ C, C },
};
var d = new string[4, 4]
{
{ D, D, D, D },
{ D, D, D, D },
{ D, D, D, D },
{ D, D, D, D },
};
var result = Combine(a, b, c, d);
This of course can be generalized to any size matrix of matrices. The concept is the same in every step of the process.
Given m x n matrices, we will try to build a resulting matrix in this arrangement:
+-----+-----+-----+
| 0,0 | ... | 0,n |
+-----+-----+-----+
| ... | | ... |
+-----+-----+-----+
| m,0 | ... | m,n |
+-----+-----+-----+
Get the dimensions of each of the slices.
rows_0 = max(rows_0_0, ..., rows_0_n)
...
rows_m = max(rows_m_0, ..., rows_m_n)
rows_result = sum(rows_0, ..., rows_m)
cols_0 = max(cols_0_0, ..., cols_m_0)
...
cols_n = max(cols_0_n, ..., cols_m_n)
cols_result = sum(cols_0, ..., cols_m)
Get the offsets for each of the matrices. Each vertical slice is offset to the left by the total amount of columns in the previous vertical slices. Each horizontal slice is offset to the down by the total amount of rows in the previous horizontal slices.
offset_0_0 = (0, 0)
...
offset_m_n = (sum(rows_0, ..., rows_m-1), sum(cols_0, ..., cols_n-1))
So now we can build up the result matrix.
public T[,] Combine<T>(T[,][,] m)
{
// get the rows
var rows = GetSliceRows(m);
var rows_result = rows.Sum();
// get the cols
var cols = GetSliceCols(m);
var cols_result = cols.Sum();
// get the offsets
var offsets = GetOffsets(rows, cols);
// fill 'er up
var result = new T[rows_result, cols_result];
Fill(result, m, offsets);
return result;
}
public int[] GetSliceRows<T>(T[,][,] m)
{
var sliceRows = new int[m.GetLength(0)];
var segments = m.GetLength(1);
for (var i = 0; i < sliceRows.Length; i++)
{
sliceRows[i] = Enumerable.Range(0, segments)
.Select(j => m[i, j].GetLength(0))
.Max();
}
return sliceRows;
}
public int[] GetSliceCols<T>(T[,][,] m)
{
var sliceCols = new int[m.GetLength(1)];
var segments = m.GetLength(0);
for (var j = 0; j < sliceCols.Length; j++)
{
sliceCols[j] = Enumerable.Range(0, segments)
.Select(i => m[i, j].GetLength(1))
.Max();
}
return sliceCols;
}
public Tuple<int, int>[,] GetOffsets(int[] rows, int[] cols)
{
var offsets = new Tuple<int, int>[rows.Length, cols.Length];
for (var i = 0; i < rows.Length; i++)
for (var j = 0; j < cols.Length; j++)
offsets[i, j] = Tuple.Create(
rows.Take(i).Sum(),
cols.Take(j).Sum()
);
return offsets;
}
public void Fill<T>(T[,] result, T[,][,] m, Tuple<int, int>[,] offsets)
{
for (var i = 0; i < m.GetLength(0); i++)
for (var j = 0; j < m.GetLength(1); j++)
Fill(result, m[i, j], offsets[i, j]);
}
public void Fill<T>(T[,] result, T[,] source, Tuple<int, int> offset)
{
for (var i = 0; i < source.GetLength(0); i++)
for (var j = 0; j < source.GetLength(1); j++)
result[offset.Item1 + i, offset.Item2 + j] = source[i, j];
}

I think you have to give array elements corresponding rowid and columnid to achieve the indexing issue of the outer matrix. Assuming you already have a Array to Matrix object conversion;
Being not sure if I undestood the rules correctly, but here is what I implemented so far:
I implemented Matrix and MatrixList classes as follows:
public class Matrix
{
public int row { get; set; }
public int column { get; set; }
public double value { get; set; }
}
public class MatrixList
{
public List<Matrix> matrixList = new List<Matrix>();
}
Using those classes, I implemented the algorithm below:
List<MatrixList> matricesMatrix = new List<MatrixList>();
init(matricesMatrix);
int totalRows = 10;//as you stated, this is already known
int totalColumns = 10;//as you stated, this is already known
List<Matrix> ResultMatrix = new List<Matrix>();
foreach (MatrixList matrixListItem in matricesMatrix)
{
for (int i = 0; i < totalRows; i++)
{
List<Matrix> matrixItemList = matrixListItem.matrixList.FindAll(s => s.row == i);
foreach(Matrix matrixItem in matrixItemList)
for (int j = 0; j < totalColumns; j++)
{
if (matrixItem.column == j)
ResultMatrix.Add(new Matrix { row = i, column = j, value = matrixItem.value });
}
}
}
where init is a method to fill the objects, implemented as follows:
private void init(List<MatrixList> matricesMatrix)
{
MatrixList ml = new MatrixList();
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
ml.matrixList.Add(new Matrix { row = i, column = j, value = i + j });
}
}
matricesMatrix.Add(ml);
}
I was on a windows forms dummy app, so used a richtextbox to test the code above.
for (int i = 0; i < totalRows; i++)
{
foreach (Matrix item in ResultMatrix)
{
if (item.row == i)
{
for (int j = 0; j < totalColumns; j++)
if (item.column == j)
richTextBox1.Text += item.value + " ";
}
}
richTextBox1.Text += Environment.NewLine;
}
and the result is:
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18
I don't have much time to give pretty numbers to array items to show beriefly at the moment, but I think you can get the idea.

Related

Using Random class to randomize 2d array

I am trying to randomize a sett of predetermine elements in a 2d array.
using System;
namespace array
{
public class tiles
{
static void Main(string[] args)
{
Random random = new Random();
int[,] tilearr = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } };
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(tilearr[i, j] + " ");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
My problem is if I do something like tilearr[i, j] = random.Next(0, 8); it randomizes the array but doesn't care if there are any duplicates of the same element.
2 6 7
1 1 3
2 7 0
what I am after is more like this:
2 4 8 1 3 0
0 3 1 5 6 8
6 7 5, 2 4 7
A simple and to the point solution would be to have a list of available numbers and then go position by position and randomly select the numbers out of the list.
Like this:
var numbers = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
for(int x = 0; x < 3; ++x) {
for(int y = 0; y < 3; ++y) {
// select a random number from the list ...
int rand = random.Next(0, numbers.Count - 1);
tilearr[x, y] = numbers[rand];
// ... and remove it from the list
numbers.RemoveAt(rand);
}
}
As user Wai Ha Lee stated in the comments a shuffle will achieve what you are looking for. I would recommend the Fisher Yates Shuffle.
public static void Shuffle<T>(Random random, T[,] array)
{
int lengthRow = array.GetLength(1);
for (int i = array.Length - 1; i > 0; i--)
{
int i0 = i / lengthRow;
int i1 = i % lengthRow;
int j = random.Next(i + 1);
int j0 = j / lengthRow;
int j1 = j % lengthRow;
T temp = array[i0, i1];
array[i0, i1] = array[j0, j1];
array[j0, j1] = temp;
}
}
I retrieved this implementation from this answer.
This should be implemented in your code like this,
using System;
namespace array
{
public class tiles
{
static void Main(string[] args)
{
Random random = new Random();
int[,] tilearr = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } };
Shuffle<int>(random, tilearr);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(tilearr[i, j] + " ");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Make sure to place the shuffle generic function within your class.
HERE is an example of my implementation on dotnetfiddle.net.
If you generate the array from scratch, it's easier to randomize a one dimensional array, and then load it into a 2D array.
static int[,] GenerateArray(int size)
{
Random r = new Random();
var arr = new int[size, size];
var values = Enumerable.Range(0, size * size).OrderBy(x => r.Next()).ToArray();
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
arr[i, j] = values[i * size + j];
return arr;
}
One way to Randomize would be flatten the 2d array, shuffle it and then recreate based on original dimension. If you want to use Linq/Extension methods, you could do the following
Random random = new Random();
int[,] tilearr = {{ 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }};
var result = tilearr.OfType<int>()
.OrderBy(x=> random.Next())
.ChunkBy(tilearr.GetLength(1))
.To2DArray();
Where ChunkBy and To2DArray are defined as
public static class Extensions
{
public static IEnumerable<IEnumerable<T>> ChunkBy<T>(this IEnumerable<T> source, int chunkSize)
{
return source
.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / chunkSize)
.Select(x => x.Select(v => v.Value));
}
public static T[,] To2DArray<T>(this IEnumerable<IEnumerable<T>> source)
{
var data = source
.Select(x => x.ToArray())
.ToArray();
var res = new T[data.Length, data.Max(x => x.Length)];
for (var i = 0; i < data.Length; ++i)
{
for (var j = 0; j < data[i].Length; ++j)
{
res[i,j] = data[i][j];
}
}
return res;
}
}
Sample Demo

How to calculate the average of each row in multidimensional array

What i want to do is get the average of each row of what the user inputs. I'm able to display the input, but not sure how to calculate an average of the three numbers in each row. What would be a solution? I'm new to C# so still learning.
Here's my code:
class Program
{
static void Main(string[] args)
{
int[,] number = new int[3, 5];
for (int i = 0; i < 3; i++)
{
for (int x = 0; x < 3; x++)
{
Console.WriteLine("Please enter number");
number[x, i] = int.Parse(Console.ReadLine());
}
}
for (int i = 0; i < 3; i++)
{
for (int x = 0; x < 3; x++)
{
Console.Write(number[x, i] + " ");
}
Console.WriteLine(" ");
Console.ReadLine();
}
}
}
You can do it something like this
for(int i = 0; i < 3; i++)
{
int Avg = 0;
for(int x = 0; x < 3; x++)
{
Console.Write(number[x, i] + " ");
Avg += number[x, i];
}
Avg = Avg / 3;
Console.Write("Average is" + Avg);
Console.WriteLine(" ");
Console.ReadLine();
}
I think you have to create a method like the following, that will accept a two dimensional array as input and iterate through its rows and further iteration will performed through its cols to find the sum of all elements in each rows and then it will be divided with number of cols, to get the average. Take a look into the method
public static void rowWiseAvg(int[,] inputArray)
{
int rows = inputArray.GetLength(0);
int cols = inputArray.GetLength(1);
for (int i = 0; i < rows; i++)
{
float rowAvg = 0;
for (int x = 0; x < cols; x++)
{
rowAvg += inputArray[i,x];
}
rowAvg = rowAvg / cols;
Console.Write("Average of row {0} is :{1}", i,rowAvg);
}
}
An additional note for you : When you are reading values for a multi-dimensional array, use outer loop to read values for the rows and inner loop for reading columns. in your case you are actually reads the columns first then gets values for each rows in a column. One more, use float / double to store the average
Here's a (mostly) LINQ solution:
var array = new int[,]
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
int count = 0;
var averages = array.Cast<int>()
.GroupBy(x => count++ / array.GetLength(1))
.Select(g => g.Average())
.ToArray();
// [2.5, 6.5, 10.5]
The simplest way is to use for loops, as described in other answers.
You can also utilize LINQ and use Enumerable.Range to make it another way:
public static class MultidimensionalIntArrayExtensions
{
// Approach 1 (using Select and Average)
public static double[] RowAverages(this int[,] arr)
{
int rows = arr.GetLength(0);
int cols = arr.GetLength(1);
return Enumerable.Range(0, rows)
.Select(row => Enumerable
.Range(0, cols)
.Select(col => arr[row, col])
.Average())
.ToArray();
}
// Approach 2 (using Aggregate)
public static double[] RowAverages(this int[,] arr)
{
int rows = arr.GetLength(0);
int cols = arr.GetLength(1);
return Enumerable.Range(0, rows)
.Select(row => Enumerable
.Range(0, cols)
.Aggregate(0.0, (avg, col) => avg + ((double)arr[row, col] / cols)))
.ToArray();
}
}
// Usage:
int[,] arr =
{
{ 1, 2, 3 },
{ 2, 3, 4 },
{ 3, 4, 5 },
{ 6, 7, 8 },
{ 1, 1, 1 }
};
double[] rowSums = arr.RowAverages(); // [2, 3, 4, 7, 1]
This code may look unreadable and non-OOP for some developers; and may seem good and laconic for others. If your belongs to the second group, use this code.

Matrix element comparison

I have some problems with comparison of matrix elements. Here is the code:
int coll = 0;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
int tmp = 0;
for (int k = 0; k < 9; k++)
{
if (matrix[i, j] == matrix[i, k])
{
tmp++;
Console.WriteLine("{0},{1} coll {2},{3}-al", i, j, i, k);
}
coll += tmp;
}
}
}
The code wants to compare the elements of an array called matrix. When 2 elements in a column are the same, I'll increase the tmp value. At the end coll will be increased by the number of the collisions of the actual element of the array. The problem is that the program will find only the collisions of the element with itself. For example, for a matrix like
1234
1342
2341
2413
the 0:0 position will collide only with itself and not with 1:0. Can anyone tell me why?
Try this logic:
class Program
{
static void Main(string[] args)
{
int[,] matrix=new int[,] {
{ 1, 2, 3, 4 },
{ 1, 3, 4, 2 },
{ 2, 3, 4, 1 },
{ 2, 4, 1, 3 } };
// This returns the #of collisions in each column
Debug.WriteLine(CheckColumn(matrix, 0)); // 2
Debug.WriteLine(CheckColumn(matrix, 1)); // 1
Debug.WriteLine(CheckColumn(matrix, 2)); // 1
Debug.WriteLine(CheckColumn(matrix, 3)); // 0
}
static int CheckColumn(int[,] matrix, int column)
{
int[] data=new int[matrix.GetLength(0)];
for(int i=0; i<data.Length; i++)
{
data[i]=matrix[i, column];
}
var hist=data.GroupBy(i => i)
.OrderByDescending(g => g.Count())
.Select(g => new { Num=g.Key, Dupes=g.Count()-1 })
.Where(h=>h.Dupes>0);
return hist.Count()>0?hist.Sum(h=>h.Dupes):0;
}
}
I used code from https://stackoverflow.com/a/10335326/380384

Combining two double[] arrays into double[,]

I frequently have two arrays that I need to combine to one matrix (same lenght and type). I was wondering whether there is a linq way that is more elegant than:
var result = new double[dt.Count, 2];
for (int i = 0; i < dt.Count; i++)
{
result[i, 0] = dts[i];
result[i, 1] = dt[i];
}
I tried
var result = dts.zip(dt, (a,b) => new{a,b})
and:
var result = dts.Concat(dt).ToArray()
But neither do what I would like to do...
There is nothing in the framework, but here is a general solution (that works for 2 or more arrays):
public static class ArrayConvert
{
public static T[,] To2DArray<T>(params T[][] arrays)
{
if (arrays == null) throw new ArgumentNullException("arrays");
foreach (var a in arrays)
{
if (a == null) throw new ArgumentException("can not contain null arrays");
if (a.Length != arrays[0].Length) throw new ArgumentException("input arrays should have the same length");
}
var height = arrays.Length;
var width = arrays[0].Length;
var result = new T[width, height];
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
{
result[i, j] = arrays[i][j];
}
return result;
}
}
Which can then be used as follows:
var convertedArray = ArrayConvert.To2DArray(new[]{1,2,3}, new[]{4,5,6}, new[]{7,8,9});
ok then use this
class Program {
static void Main(string[] args) {
double[,] x = { { 1, 2, 3 }, { 4, 5, 6 } };
double[,] y = { { 7, 8, 9 }, { 10, 11, 12 } };
var xy = new StitchMatrix<int>(x, y);
Console.WriteLine("0,0=" + xy[0, 0]); // 1
Console.WriteLine("1,1=" + xy[1, 1]); // 5
Console.WriteLine("1,2=" + xy[1, 2]); // 6
Console.WriteLine("2,2=" + xy[2, 2]); // 9
Console.WriteLine("3,2=" + xy[3, 2]); // 12
}
}
class StitchMatrix<T> {
private T[][,] _matrices;
private double[] _lengths;
public StitchMatrix(params T[][,] matrices) {
// TODO: check they're all same size
_matrices = matrices;
// call uperbound once for speed
_lengths = _matrices.Select(m => m.GetUpperBound(0)).ToArray();
}
public T this[double x, double y] {
get {
// find the right matrix
double iMatrix = 0;
while (_lengths[iMatrix] < x) {
x -= (_lengths[iMatrix] + 1);
iMatrix++;
}
// return value at cell
return _matrices[iMatrix][x, y];
}
}
}
Here is another solution. I "prepare" the input for LINQ processing. Don't sure that this is elegant, but it is LINQ:
// the input
double[] dts = { 1, 2, 3, 4, 5 };
double[] dt = { 10, 20, 30, 40, 50 };
// list of lists, for iterating the input with LINQ
double[][] combined = { dts, dt };
var indexes = Enumerable.Range(0, dt.Length);
var subIndexes = Enumerable.Range(0, 2);
// the output
var result = new double[dt.Length, 2];
var sss = from i in indexes
from j in subIndexes
select result[i, j] = combined[j][i];
// just to activate the LINQ iterator
sss.ToList();
I suggest against doing it directly in LINQ. You can write a generic method to do it for you, something like:
public static T[,] To2DArray<T>(this T[][] arr)
{
if (arr.Length == 0)
{
return new T[,]{};
}
int standardLength = arr[0].Length;
foreach (var x in arr)
{
if (x.Length != standardLength)
{
throw new ArgumentException("Arrays must have all the same length");
}
}
T[,] solution = new T[arr.Length, standardLength];
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < standardLength; j++)
{
solution[i, j] = arr[i][j];
}
}
return solution;
}
I know that wasnt the question but the most elegant answer is to use f#:
let combinearrays (arr1:array<'a>) (arr2:array<'a>) =
let rws = arr1|> Array.length
Array2D.init rws 2 (fun i j -> match j with |0 -> arr1.[i] |1 -> arr2.[i])
From John see here
Here is the solution Convert two 1d Arrays in one 2D Array which I developed for my own use.
(from a1 in array1.Select((n,index)=>new{Index=index,c1=n}).ToList()
join a2 in array2.Select((n,index)=>new {Index=index,c2=n}).ToList() on a1.Index equals a2.Index
select new {c1,c2}
).ToArray()

matrix to array c#

Which would be the most efficient way to convert a squared matrix like
1 2 3
4 5 6
7 8 9
into
[1 2 3 4 5 6 7 8 9]
in c#
I was doing
int[,] array2D = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int[] array1D = new int[9];
int ci=0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
array1D[ci++] = array2D[i, j]);
}
}
LINQ makes this trivial.
int[,] array2d = ...;
var array1d = array2d.Cast<int>().ToArray();
Otherwise, your way is adequate but could be generalized:
int[,] array2d = ...;
var rows = array2d.GetLength(0);
var cols = array2d.GetLength(1);
var array1d = new int[rows * cols];
var current = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
array1d[current++] = array2d[i, j];
}
}
Or even:
int[,] array2d = ...;
var array1d = new int[array2d.GetLength(0) * array2d.GetLength(1)];
var current = 0;
foreach (var value in array2d)
{
array1d[current++] = value;
}
As Jeff said, LINQ makes this trivial. OfType<>() should generally be a little faster than Cast<> though:
array1D = array2D.OfType<int>().ToArray();
The implementation of OfType<> however will still suffer from boxing/unboxing penalties, as #phoog mentioned.
Just for the fun of it, if you want a fast LINQ-based solution (avoiding the cost of boxing) you could use this small extension method:
static class LinqEx
{
public static IEnumerable<T> Flatten<T>(this T[,] matrix)
{
foreach (var item in matrix) yield return item;
}
}
Or this, based on Jeff's 2nd solution:
public static IEnumerable<T> Flatten<T>(this T[,] matrix)
{
var rows = matrix.GetLength(0);
var cols = matrix.GetLength(1);
for (var i = 0; i < rows;i++ )
{
for (var j = 0; j < cols; j++ )
yield return matrix[i, j];
}
}
usage:
int[,] array2D = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int[] array1D = array2D.Flatten().ToArray();
I didn't fully profile this but I expect this will get you much better performance than the built-in options based on LINQ/IEnumerable. Jeff's second solution will however always be the fasted, it seems.
Alternate solution using Buffer.BlockCopy:
int[,] array2D = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int[] array1D = new int[ array2D.Length ];
Buffer.BlockCopy(array2D, 0, array1D, 0, array1D.Length * sizeof(int));
You're always better off allocating the complete result array in one hit, then copying the data in.
You should find the total size like this;
var size = arrays.Sum(a=> a.Length);
var result = new int[size];
And then copy the arrays using Array.CopyTo, instead of looping yourself;
var cursor = 0;
foreach(var a in arrays) {
a.CopyTo(result, cursor);
cursor += a.Length;
}
Array.CopyTo will be faster than your own loop; at least, not slower. It will probably use C's memcpy function internally to do a low-level block copy. This is as efficient as you can be.

Categories