Hello everyone I am new to programming in c#, what I am going to do is that I want to convert the 2D string array to 2D integer array, the reason for this conversion is that I want to pass that integer array to another method for some calculation. Thanks in advance for helping.
public void Matrix()
{
int a = int.Parse(txtRowA.Text);
int b = int.Parse(txtColA.Text);
Random rnd = new Random();
int[,] matrixA = new int[a, b];
string matrixString = "";
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
matrixA[i, j] = rnd.Next(1, 100);
matrixString += matrixA[i, j];
matrixString += " ";
}
matrixString += Environment.NewLine;
}
txtA.Text = matrixString;
txtA.TextAlign = HorizontalAlignment.Center;
}
Your code is actually pretty close. Try this:
private Random rnd = new Random();
public int[,] Matrix(int a, int b)
{
int[,] matrixA = new int[a, b];
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
matrixA[i, j] = rnd.Next(1, 100);
}
}
return matrixA;
}
What you have there is a function that does not rely on any WinForms controls and will produce your int[,] quickly and efficiently.
Let the calling code work with the WinForms controls:
int a = int.Parse(txtRowA.Text);
int b = int.Parse(txtColA.Text);
int[,] matrix = Matrix(a, b);
Now you can pass the matrix to anything that requires an int[,].
If you need to display the matrix then create a separate function for that:
public string MatrixToString(int[,] matrix)
{
StringBuilder sb = new();
for (int i = 0; i < matrix.GetLength(0); i++)
{
string line = "";
for (int j = 0; j < matrix.GetLength(1); j++)
{
line += $"{matrix[i, j]} ";
}
sb.AppendLine(line.Trim());
}
return sb.ToString();
}
Your calling code would look like this:
int a = int.Parse(txtRowA.Text);
int b = int.Parse(txtColA.Text);
int[,] matrix = Matrix(a, b);
UseTheMatrix(matrix);
txtA.Text = MatrixToString(matrix);
txtA.TextAlign = HorizontalAlignment.Center;
A sample run produces:
You can use List as a helper to store the elements of the array extracted from the string, but first I replaced the SPACE between elements in your string with a special character '|' as a separator to make it easier to extract the numbers from the string.
you can do somthing like this:
public static int[,] ConvertToInt(string matrix)
{
List<List<int>> initial = new List<List<int>>();
int lineNumber = 0;
int currenctIndex = 0;
int lastIndex = 0;
int digitCount = 0;
initial.Add(new List<int>());
foreach (char myChar in matrix.ToCharArray())
{
if (myChar == '|')
{
initial[lineNumber].Add(int.Parse(matrix.Substring(lastIndex, digitCount)));
lastIndex = currenctIndex+1;
digitCount = 0;
}
else
digitCount++;
if (myChar == '\n')
{
lineNumber++;
if(currenctIndex < matrix.Length-1)
initial.Add(new List<int>());
}
currenctIndex++;
}
int[,] myInt = new int[initial.Count, initial[0].Count];
for (int i = 0; i < initial.Count; i++)
for (int j = 0; j < initial[i].Count; j++)
myInt[i, j] = initial[i][j];
return myInt;
}
I am trying to create an array of 6 arrays each containing 4 arrays, each of those 4 arrays with different shape.
Like in the form below:
[[array([[1,2,3,4,6,5],
[5,6,6,7,7,5],
[6,6,5,5,6,5],
[4,6,6,7,7,5],
[5,6,6,7,7,5],
[6,6,5,5,6,5],
[6,6,5,5,6,5],]),
array([[9,9,9],
[9,9,9],
[6,6,6],
[3,3,6],
[6,6,6]]),
array([[6],
[7],
[7],
[7],
[7]]),
array([[7,8,8,8,8]])],
[[array([[1,2,3,4,6,5],
[5,6,6,7,7,5],
[6,6,5,5,6,5],
[4,6,6,7,7,5],
[5,6,6,7,7,5],
[6,6,5,5,6,5],
[6,6,5,5,6,5],]),
array([[9,9,9],
[9,9,9],
[6,6,6],
[3,3,6],
[6,6,6]]),
array([[6],
[7],
[7],
[7],
[7]]),
array([[7,8,8,8,8]])]]
..........
I have this code but I am doing something wrong, I do not get the desired shape.
Can somebody please help me with this?
int populationSize = 6;
double[][][][] population = new double[populationSize][][][];
int value = 3;
for (int i = 0; i < populationSize; i++)
{
population[i] = new double[4][][];
for (int j = 0; j < 4; j++)
{
const int rows = 7, cols = 6;
population[i][j] = new double[rows][];
for (int k = 0; k < rows; k++)
{
population[i][j][k] = new double[cols];
for (int m = 0; m < cols; m++)
{
population[i][j][k][m] = value;
}
}
}
}
const int rows = 7, cols = 6;
population[j][j] = new double[5][];
for (int k = 0; k < rows; k++)
you are using the same indexes for population[j][j], that is probably incorrect.
You are also specifying 7 rows, but create a matrix of size 5. That is probably incorrect.
As Scopperloit points out, you probably want:
const int rows = 7, cols = 6;
population[i][j] = new double[rows][];
for (int k = 0; k < rows; k++)
Overall when debugging problems. Run the code in a debugger and inspect the values against your expectations. Index out of range exceptions are usually very easy to debug:
What is the index?
What is the length of the array?
Is the index or array length incorrect?
I managed to solve my problem.
THis function creates an array of arrays of arrays of different shapes according to another array, and fills it with random normal standard distribution values :
int[,] netshape = new int[,] { { 3, 4}, { 4, 5}, { 4, 1 }, { 1, 4} }; // The shape of the network
public double[][][][] CreateNetwork(int[,] netshape,int populationSize )
{
normal = new MathNet.Numerics.Distributions.Normal(0, 1);// Generate random normal standard distribution
int rows, cols ;
double[][][][] population = new double[populationSize][][][];
for (int i = 0; i < populationSize; i++)
{
population[i] = new double[4][][];
for (int j = 0; j < 4; j++)
{
rows = netshape[j, 0];
cols = netshape[j, 1];
population[i][j] = new double[rows][];
for (int k = 0; k < rows; k++)
{
population[i][j][k] = new double[cols];
for (int m = 0; m < cols; m++)
{
population[i][j][k][m] = normal.Sample(); ;
}
}
}
}
return population;
}
Suppose a have a MxNx3 matrix
byte [,,] myMatrix= new byte[sizeRow, sizeCol, 3];
How do I access a single band of it (for read and write purposes)? Something like:
singleBand = myMatrix[:allRows: , :allCols: , :desiredBand:];
On the left is what I have, of the right is what I want to access (for example).
int M=10;
int N=20;
var test = new byte[3][,] { new byte[N,M],new byte[N,M],new byte[N,M]};
var band1 = test[1]; //its green
band1[2, 2] = 99;
If you can't change the type of myMatrix, then you can use the following code:
byte [,,] myMatrix= new byte[sizeRow, sizeCol, 3];
var singleBand = new byte[sizeRow, sizeCol];
var band = 1;
for (var i = 0; i < sizeRow; i++) {
for (var j = 0; j < sizeCol; j++) {
singleBand[i, j] = myMatrix[i, j, band];
}
}
But if you can change it, then probably Zeromus' solution is better, since you can more easily manipulate with what you call the band.
You simply need to loop through your required array elements and extract the "band" that you need.
// Create a three-dimensional array.
int[, ,] threeDimensional = new int[3, 3, 3];
// Set the first "Band" to 9
threeDimensional[0,0,1] = 9;
threeDimensional[1,0,1] = 9;
threeDimensional[2,0,1] = 9;
threeDimensional[0,1,1] = 9;
threeDimensional[1,1,1] = 9;
threeDimensional[2,1,1] = 9;
threeDimensional[0,2,1] = 9;
threeDimensional[1,2,1] = 9;
threeDimensional[2,2,1] = 9;
// Loop over each dimension's length.
for (int i = 0; i < threeDimensional.GetLength(2); i++)
{
for (int y = 0; y < threeDimensional.GetLength(1); y++)
{
for (int x = 0; x < threeDimensional.GetLength(0); x++)
{
Console.Write(threeDimensional[x, y, i]);
}
Console.WriteLine();
}
Console.WriteLine();
}
Your only option is to access each "cell" in the band one at a time and extract it to some other location. In my case i just put them out to the display.
I currently want a 2D array (4,4) to look like this:
outArr = new string[4, 4]
{
{"0","0","0","0" },
{"0","0","0","0" },
{"0","0","0","0" },
{"0","0","0","0" }
};
However, I'm unsure of how to do with in code where the array sizes could be dynamic at runtime (i.e. 3,5 or 10,10)
I found this example on how to create the array dynamically (for an int array):
int[,] myArray=new int[(int)s[0],(int)s[2]];
myArray[0, 0] = 2;
Console.WriteLine(myArray[0, 0]);
Console.ReadLine();
But I want to know how to create the elements "0" in my array dynamically.
Try this:
var r = 7;
var c = 4;
var outArr = new string[r, c];
for (var i = 0; i < r; i++)
for (var j = 0; j < c; j++)
outArr[i, j] = "0";
That gives me:
I am trying to create a list of arrays but every time I add a new array in a loop it overrides the values of the other arrays in the list.
int p = 5;
for (int j = 1; j < numComputerHands; j++)
{
int i = 0;
while (i < 5)
{
computerHand[i] = getDeck[p];
p++;
i++;
}
allComputerHands.Add(computerHand);
}
Every time you call Add(), you're just adding another reference to the same instance of the array. In the end, you have numComputerHands number of references to the same array in your List.
You'll need to create a new instance at the start of each iteration.
I'm not sure what type computerHand is, so I'll make it an array of int below.
int p = 5;
for (int j = 1; j < numComputerHands; j++)
{
computerHand = new int[5]; // change 'int' as needed
int i = 0;
while (i < 5)
{
computerHand[i] = getDeck[p];
p++;
i++;
}
allComputerHands.Add(computerHand);
}