How do I Transpose a multi dimensional array? - c#

I think this might be a pretty simple question, but I haven't been able to figure it out yet. If I've got a 2-dimensional array like so:
int[,] matris = new int[5, 8] {
{ 1, 2, 3, 4, 5,6,7,8 },
{9,10,11,12,13,14,15,16},
{ 17,18,19,20,21,22,23,24 },
{ 25,26,27,28,29,30,31,32 },
{ 33,34,35,36,37,38,39,40 },
};
and a for loop, like this:
for (int r = 0; r < 5; r++)
{
for (int j = 0; j < 8; j++)
Console.Write("{0} ", matris[r, j]);
Console.WriteLine();
}
So with this code I am printing out the multi dimensional array. But how do I print out a transpose of the array?

Just change your loops with each other:
for (int j = 0; j < 8; j++)
{
for (int r = 0; r < 5; r++)
Console.Write("{0} ", matris[r, j]);
Console.WriteLine();
}
Creating new array:
var newArray = new int[8, 5];
for (int j = 0; j < 8; j++)
for (int r = 0; r < 5; r++)
newArray[j, r] = matris[r, j];

You just need to do this:
for (int r = 0; r < 8; r++)
{
for (int j = 0; j < 5; j++)
Console.Write("{0} ", matris[j, r]);
Console.WriteLine();
}

Related

Im trying to fill a 2d 20x20 array with the multiplication table of 1 through 20

So far I have,
int[,] array2d = new int[20, 20];
for (int i = 1; i < array2d.GetLength(0); i++)
{
for (int j = 1; j < array2d.GetLength(1); j++)
{
array2d[i, j] = i * j ;
Console.WriteLine(array2d[i, j]);
}
}
but this is skipping some quite a few numbers, I tried to fix it by checking if I is <= but that throws a IndexOutOfRangeException
Is there some point where I made a major error? or is it a simple one.
Arrays are zero-based by default; so your array is [0..19, 0..19]; however, you want a different range: [1..20, 1..20]. We should not mix them: either (better choice)
int[,] array2d = new int[20, 20];
// i, j - array indexes
for (int i = 0; i < array2d.GetLength(0); i++)
{
for (int j = 0; j < array2d.GetLength(1); j++)
{
// since i, j are array indexes we multiply (i + 1) * (j + 1)
array2d[i, j] = (i + 1) * (j + 1);
Console.Write($"{array2d[i, j],3} ");
}
Console.WriteLine();
}
Or
int[,] array2d = new int[20, 20];
// i, j are values to be multiplied
for (int i = 1; i <= array2d.GetLength(0); i++)
{
for (int j = 1; j <= array2d.GetLength(1); j++)
{
// since i, j are values we have to compute array's indexes: i - 1, j - 1
array2d[i - 1, j - 1] = i * j;
Console.Write($"{array2d[i - 1, j - 1],3} ");
}
Console.WriteLine();
}
int[,] array2d = new int[20, 20];
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
array2d[i, j] = (i+1) * (j+1) ;
Console.WriteLine(array2d[i, j]);
}
}
Your problem was that when you define array like new int[20] then it can be iterated from 0 to 19, when you try to acces its value at index 20 you get this exception because index 20 doesnt exist in this array.
When you call arrays .GetLength method it returns its count of fields which is 20, not its maximum index which is 19.

how to remake 2d array random unique numbers as 3d?

static void Main(string[] args)
{
Random r = new Random();
int[,] x = new int[10,8];
int[] temp = new int[x.Length];
// two dimensional array and i want for three dimensional array
for(int i = 0; i < temp.Length; i++)
{
temp[i] = r.Next(10, 100);
for(int j = 0; j < i; j++)
{
if(temp[i] == temp[j])
{
i--;
break;
}
}
}
for(int i = 0, index = 0; i < x.GetLength(0); i++)
{
for(int j = 0; j < x.GetLength(1); j++)
{
x[i, j] = temp[index++]; //two dimensional array unique numbers
Console.Write(x[i, j] + " ");
}
}
// i want do it as for 3D and 4 D array unique numbers like that method what can i change or add?
It's pretty straight forward to do what you're doing for higher dimensions.
Here's my code for 3D:
var r = new Random();
int [,,] x = new int[10, 8, 8];
var count =
Enumerable
.Range(0, x.Rank)
.Select(y => x.GetLength(y))
.Aggregate((y, z) => y * z);
var values =
Enumerable
.Range(10, count)
.OrderBy(y => r.Next())
.ToArray();
var v = 0;
for (var i = x.GetLowerBound(0); i <= x.GetUpperBound(0); i++)
for (var j = x.GetLowerBound(1); j <= x.GetUpperBound(1); j++)
for (var k = x.GetLowerBound(2); k <= x.GetUpperBound(2); k++)
x[i, j, k] = values[v++];
To change it to 4D these lines change:
int [,,,] x = new int[10, 8, 8, 12];
// ...
var v = 0;
for (var i = x.GetLowerBound(0); i <= x.GetUpperBound(0); i++)
for (var j = x.GetLowerBound(1); j <= x.GetUpperBound(1); j++)
for (var k = x.GetLowerBound(2); k <= x.GetUpperBound(2); k++)
for (var l = x.GetLowerBound(3); l <= x.GetUpperBound(3); l++)
x[i, j, k, l] = values[v++];
Now, in this code I have explicitly called GetLowerBound as well as GetUpperBound as it is possible in .NET code to have a non-zero based array.
Also, rather than repeatedly re-try getting random numbers until you have unique numbers I simply generated a sequence of unique numbers and then randomly sorted them. That's a little different from your original code. You needed 80 (10 x 8) random values and you were choosing from values ranging from 10 to 99 inclusive. So you had some holes in your numbers.
Random r = new Random();
int[,,] x = new int[10, 8, 8];
int[] temp = new int[x.Length];
#region one dimensional array unique numbers.
for (int i = 0; i < temp.Length; i++)
{
temp[i] = r.Next(10, 650);
for (int j = 0; j < i; j++)
{
if (temp[i] == temp[j])
{
i--;
break;
}
}
}
#endregion
for (int i = 0, index = 0; i < x.GetLength(0); i++)
{
for (int j = 0; j < x.GetLength(1); j++)
{
for (int k = 0; k < x.GetLength(2); k++)
{
x[i, j, k] = temp[index++];
Console.Write(x[i, j, k] + " ");
}
Console.WriteLine();
}
}// i think it's correct code i've changed it

Getting numbers from a matrix

Hi my problem is I can't get the numbers with more than 2 digits from this matrix if anyone can help I would appriciate it here i my code :
Console.Write("x: ");
int x = int.Parse(Console.ReadLine());
Console.Write("y: ");
int y = int.Parse(Console.ReadLine());
int[,] arr = new int[x, y];
int[,] arr2 = new int[x, y];
Random rand = new Random();
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
int randNUm = rand.Next(0, 20);
arr[i, j] = randNUm;
Console.Write(arr[i, j] + " ");
if (arr[i, j] >= 10)
{
arr2[i,j] = arr[i,j]
}
}
}
Actually you have done your job, all it remains is to display the results. If
the task specifies that the printing must be done in 2 steps as you imply,
try:
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
int randNUm = rand.Next(0, 20);
arr[i, j] = randNUm;
Console.Write(arr[i, j] + " ");
if (arr[i, j] >= 10)
{
arr2[i,j] = arr[i,j]
}
}
}
Console.WriteLine("---Proceeding to 2 digit numbers---");
for (int i = 0; i < arr2.GetLength(0); i++)
{
for (int j = 0; j < arr2.GetLength(1); j++)
{
Console.Write(arr2[i, j] + " ");
}
}
EDIT : Consider what Henk comments and try to optimise your solution.

How can I use hopfield network to learn more patterns?

Is there any relation between number of neurons and ability of Hopfield network to recognize patterns?
I write neural network program in C# to recognize patterns with Hopfield network. My network has 64 neurons. When I train network for 2 patterns, every things work nice and easy, but when I train network for more patterns, Hopfield can't find answer!
So, according to my code, how can I use Hopfield network to learn more patterns?
Should I make changes in this code?
There is my train() function:
public void Train(bool[,] pattern)
{
//N is number of rows in our square matrix
//Convert input pattern to bipolar
int[,] PatternBipolar = new int[N, N];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
{
if (pattern[i, j] == true)
{
PatternBipolar[i, j] = 1;
}
else
{
PatternBipolar[i, j] = -1;
}
}
//convert to row matrix
int count1 = 0;
int[] RowMatrix = new int[(int)Math.Pow(N, 2)];
for (int j = 0; j < N; j++)
for (int i = 0; i < N; i++)
{
RowMatrix[count1] = PatternBipolar[i, j];
count1++;
}
//convert to column matrix
int count2 = 0;
int[] ColMatrix = new int[(int)Math.Pow(N, 2)];
for (int j = 0; j < N; j++)
for (int i = 0; i < N; i++)
{
ColMatrix[count2] = PatternBipolar[i, j];
count2++;
}
//multiplication
int[,] MultipliedMatrix = new int[(int)Math.Pow(N, 2), (int)Math.Pow(N, 2)];
for (int i = 0; i < (int)Math.Pow(N, 2); i++)
for (int j = 0; j < (int)Math.Pow(N, 2); j++)
{
MultipliedMatrix[i, j] = ColMatrix[i] * RowMatrix[j];
}
//cells in the northwest diagonal get set to zero
for (int i = 0; i < (int)Math.Pow(N, 2); i++)
MultipliedMatrix[i, i] = 0;
// WightMatrix + MultipliedMatrix
for (int i = 0; i < (int)Math.Pow(N, 2); i++)
for (int j = 0; j < (int)Math.Pow(N, 2); j++)
{
WeightMatrix[i, j] += MultipliedMatrix[i, j];
}
And there is Present() function (this function is used to return answer for a given pattern):
public void Present(bool[,] Pattern)
{
int[] output = new int[(int)(int)Math.Pow(N, 2)];
for (int j = 0; j < N; j++)
for (int i = 0; i < N; i++)
{
OutputShowMatrix[i, j] = 0;
}
//convert bool to binary
int[] PatternBinary = new int[(int)Math.Pow(N, 2)];
int count = 0;
for (int j = 0; j < N; j++)
for (int i = 0; i < N; i++)
{
if (Pattern[i, j] == true)
{
PatternBinary[count] = 1;
}
else
{
PatternBinary[count] = 0;
}
count++;
}
count = 0;
int activation = 0;
for (int j = 0; j < (int)Math.Pow(N, 2); j++)
{
for (int i = 0; i < (int)Math.Pow(N, 2); i++)
{
activation = activation + (PatternBinary[i] * WeightMatrix[i, j]);
}
if (activation > 0)
{
output[count] = 1;
}
else
{
output[count] = 0;
}
count++;
activation = 0;
}
count = 0;
for (int j = 0; j < N; j++)
for (int i = 0; i < N; i++)
{
OutputShowMatrix[i, j] = output[count++];
}
In below images I trained Hopfield for characters A and P and when input patterns are like A or P, network recognize them in true way
Then I train it for character C:
This is where every things go wrong!
Now if I enter pattern like C, this issue happen:
And if enter pattern like A, see what happen:
And if train more patterns, whole of grid become black!
I've spotted only one mistake in your code: you perform only one iteration of node value calculation, without verifying if the values have converged. I've fixed this method like this:
public bool[,] Present(bool[,] pattern)
{
bool[,] result = new bool[N, N];
int[] activation = new int[N * N];
int count = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
{
activation[count++] = pattern[i, j] ? 1 : 0;
}
bool convergence = false;
while (!convergence)
{
convergence = true;
var previousActivation = (int[])activation.Clone();
for (int i = 0; i < N * N; i++)
{
activation[i] = 0;
for (int j = 0; j < N * N; j++)
{
activation[i] += (previousActivation[j] * WeightMatrix[i, j]);
}
activation[i] = activation[i] > 0 ? 1 : 0;
if (activation[i] != previousActivation[i])
{
convergence = false;
}
}
}
count = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
{
result[i, j] = activation[count++] == 1;
}
return result;
}
This slightly improves the results, however probably should also be improved to calculate the values asynchronously to avoid cycles.
Unfortunately, this still introduces the behaviour you've described. This is results from the phenomena called spurious patterns. For the network to learn more than one pattern consider training it with a Hebb rule. You can read about the spurious patterns, stability and learning of the Hopfield network here and here.

Multiplying two matrices but getting the wrong answer

This is my matrix code. I am multiplying two matrices. One of the matrices is scalar (meaning diagonal elements are the same), but when I run this code, I am getting the wrong answer.
static void Main(string[] args)
{
int[,] matrix1 = new int[3, 3];
int[,] matrix2 = new int[3, 3];
int[,] result = new int[3, 3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.WriteLine("Enter 1st Matrix: ");
matrix1[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.ReadLine();
for (int k = 0; k < 3; k++)
{
for (int l = 0; l < 3; l++)
{
Console.WriteLine("Enter 2nd Matrix: ");
matrix2[k, l] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine();
Console.WriteLine("Matrix 1: ");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(matrix1[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine("Matrix 2: ");
for (int k = 0; k < 3; k++)
{
for (int l = 0; l < 3; l++)
{
Console.Write(matrix2[k, l] + " ");
}
Console.WriteLine();
}
Console.WriteLine("Matrix 1 * Matrix 2: ");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
result[i, j] = result[i, j] + matrix1[i, j] * matrix2[i, j];
Console.Write(result[i, j] + " ");
}
Console.WriteLine();
}
Console.ReadLine();
Console.ReadLine();
}
You're not multiplying the matrices, you're multiplying their values. See https://en.wikipedia.org/wiki/Matrix_multiplication
E.g. {{1, 1}, {0, 0}} x {{1, 0}, {1, 0}} should result in {{2, 0}, {0, 0}}, not {{1, 0}, {0, 0}}.
Here is the right code for matrix multiplication (note that it has the complexity of O(n^3), not O(n^2)):
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
result[i, j] = 0;
for(int k = 0; k < 3; k++)
{
result[i, j] = result[i, j] + matrix1[i, k] * matrix2[k, j];
}
Console.Write(result[i, j] + " ");
}
Console.WriteLine();
}

Categories

Resources