System.IndexOutOfRangeException Sum of sums 0 - c#

I'm sorry for this simple question.But i'm beginner.
I'm try to finds those subsets whose sum is 0 which numbers are in an array.
What is wrong with my subset.Length.I have debuged it i think logic is fine.But every time a took System.IndexOutOfRangeException. I tried subset.Length-1. Result didn't changed.
The exception is given in if (subset[m] == 0) statement's line.
Thanks.
int[] subset={-3,-2,1,1,8} // the sum of -2, 1 and 1 is 0
for (int i = 0; i < subset.Length; i++)
{
for (int j = 0; i < subset.Length; j++)
{
for (int k = 0; i < subset.Length; k++)
{
for (int l = 0; i < subset.Length; l++)
{
for (int m = 0; i < subset.Length; m++)
{
if (subset[m] == 0)
{
Console.WriteLine("The subset of sum of zero:{0}", subset[m]);
}
if (subset[l] + subset[m] == 0)
{
Console.WriteLine("The subset of sum of zero:{0},{1}", subset[m], subset[l]);
}
if (subset[l] + subset[m] + subset[k] == 0)
{
Console.WriteLine("The subset of sum of zero:{0},{1},{2}", subset[m], subset[l], subset[k]);
}
if (subset[l] + subset[m] + subset[k] + subset[j] == 0)
{
Console.WriteLine("The subset of sum of zero:{0},{1},{2}", subset[m], subset[l], subset[k], subset[j]);
}
if (subset[l] + subset[m] + subset[k] + subset[j] + subset[i] == 0)
{
Console.WriteLine("The subset of sum of zero:{0},{1},{2},{3}", subset[m], subset[l], subset[k], subset[j], subset[i]);
}

Here is the logical mistake:
for (int i = 0; i < subset.Length; i++)
{
for (int j = 0; i < subset.Length; j++)
{
for (int k = 0; i < subset.Length; k++)
{
for (int l = 0; i < subset.Length; l++)
{
for (int m = 0; i < subset.Length; m++)
it should be:
for (int i = 0; i < subset.Length; i++)
{
for (int j = 0; j < subset.Length; j++)
{
for (int k = 0; k < subset.Length; k++)
{
for (int l = 0; l < subset.Length; l++)
{
for (int m = 0; m < subset.Length; m++)

Note your conditions in the for loops: it is always i < subset.Length. But you should compare against the respective loop variables instead: j, k, l or m. (Copy'n'paste error?)
In your code, the for loops for j/k/l/m are infinite because i will never be incremented while these loops iterate...

Your for loops terminating condition is always i < subset.Length, change it to relevant iteration variable, i.e. for (int m = 0; m < subset.Length; m++)
for (int j = 0; j < subset.Length; j++)
{
for (int k = 0; k < subset.Length; k++)
{
for (int l = 0; l < subset.Length; l++)
{
for (int m = 0; m < subset.Length; m++)

Related

Index out of range for multi dimensional array

I'm getting the error that the index for my array is out of range.
I define a 3D array like this:
Button[, ,] posAr_ItemManager = new Button[maxRows, maxColumns, maxCategories];
Where maxRows, maxColumns and maxCategories are all constant integers.
I then want to loop through this whole array, i do it using nested loops as shown here:
for (int i = 0; i < posAr_ItemManager.GetLength(0); i++)
{
for (int j = 0; i < posAr_ItemManager.GetLength(1); j++)
{
for (int z = 0; i < posAr_ItemManager.GetLength(2); z++)
{
if (posAr_ItemManager[i,j,z] != null)
{
Button but = posAr_ItemManager[i, j, z];
but.Width = itemWidth;
but.Height = itemHeight;
but.SetValue(Canvas.LeftProperty, itemPanelX + (i + 1) * butSpacing + i * itemWidth);
but.SetValue(Canvas.TopProperty, itemPanelY + (i+1)*butSpacing + i* itemHeight);
}
}
}
}
When I run the program it gives the out of range error where I check if the item is not equal to null.
I have no idea why it does it as I thought my declaration of the 3D array is correct and the nested loop as well? thanks in advance!
for (int i = 0; i < posAr_ItemManager.GetLength(0); i++)
{
for (int j = 0; j < posAr_ItemManager.GetLength(1); j++)
{
for (int z = 0; z < posAr_ItemManager.GetLength(2); z++)
look very carefully at the middle tests. Also, consider hoisting the GetLength tests so you only do them once per dimension. Perhaps:
int rows = posAr_ItemManager.GetLength(0), columns = posAr_ItemManager.GetLength(1),
categories = posAr_ItemManager.GetLength(2);
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < columns; col++)
{
for (int cat = 0; cat < categories; cat++)
You're using "i" to check if the loop is over in each of the dimensions. Change it to corresponded i,j,z.
for (int i = 0; i < posAr_ItemManager.GetLength(0); i++)
{
for (int j = 0; **j** < posAr_ItemManager.GetLength(1); j++)
{
for (int z = 0; **z** < posAr_ItemManager.GetLength(2); z++)

Run for loop parallel

I'm using C#.
I have the following for loop that make some type of brute force (trying all the combines):
const int N = 3 * 255 * 300;
for (var i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
for (int k = 0; k < N; k++)
{
//Do something
}
}
}
I want to be able to run this for loop parallel.
What I'm tried:
tasks[0] = Task.Factory.StartNew(() =>
{
for (var i = 0; i < 40000; i++)
{
for (int j = 0; j < N; j++)
{
for (int k = 0; k < N; k++)
{
//Do Something
}
}
}
});
tasks[1] = Task.Factory.StartNew(() =>
{
for (var i = 39999; i < 80000; i++)
{
for (int j = 0; j < N; j++)
{
for (int k = 0; k < N; k++)
{
//Do Something
}
}
}
});
tasks[2] = Task.Factory.StartNew(() =>
{
for (var i = 79999; i < 150000; i++)
{
for (int j = 0; j < N; j++)
{
for (int k = 0; k < N; k++)
{
//Do Something
}
}
}
});
tasks[3] = Task.Factory.StartNew(() =>
{
for (var i = 149999; i < N; i++)
{
for (int j = 0; j < N; j++)
{
for (int k = 0; k < N; k++)
{
//Do Something
}
}
}
});
But It Isn't really help for me, and i dont understand how to do it with j,k,
How can I make this for loop to run parallel at the best way?
Thanks!!
This might help
const int N = 3 * 255 * 300;
Parallel.For(0, N, i =>
{
for (int j = 0; j < N; j++)
{
for (int k = 0; k < N; k++)
{
//Do something
}
}
});

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.

Index out of range exception in 2D Array (C#)

char[,] map = new char[10, 20];
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; i < map.GetLength(1); j++)
map[i, j] = '.';
}
I just simply want to make all the elements of map[i,j] to be a point , but always when I try to run it the compiler says: Index out of range exception. Maybe it's a stupid question but I had to ask it.
See the i in your j-loop
for (int j = 0; j < map.GetLength(1); j++)
You use i instead of j look at this:
char[,] map = new char[10, 20];
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
map[i, j] = '.';
}
}

How do I Transpose a multi dimensional array?

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();
}

Categories

Resources