I want to store string into string array, but it shows error.
Here is my code:
List <ResponseObject> myresponse =JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
string [] DomainArray;
for (int i = 0; i < myresponse.Count; i++)
{
for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
{
DomainArray = myresponse[i].EmailAddressSuffixes[j];
}
}
You are trying to assign the DomainArray (which is an array of strings) to a single string.
Try this, it adds all the values to a list then converts to list to an array:
List<ResponseObject> myresponse = JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
List<string> DomainList = new List<string>();
for (int i = 0; i < myresponse.Count; i++)
{
for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
{
DomainList.Add(myresponse[i].EmailAddressSuffixes[j]);
}
}
var DomainArray = DomainList.ToArray();
List <ResponseObject> myresponse =JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
var DomainArray = new List<string>();
for (int i = 0; i < myresponse.Count; i++)
{
for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
{
DomainArray.Add( myresponse[i].EmailAddressSuffixes[j] );
}
}
Then you can get the array (if required) using DomainArray.ToArray()
Since arrays are not dynamic, think about working with lists:
List <ResponseObject> myresponse =JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
List<string> DomainArray = new List<string>();
for (int i = 0; i < myresponse.Count; i++)
{
for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
{
DomainArray.add(myresponse[i].EmailAddressSuffixes[j]);
}
}
I made two changes in my above code. first from #Hugo and second from #Roma
List <ResponseObject> myresponse = JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
List<string> DomainArray = new List<string>();
for (int i = 0; i < myresponse.Count; i++)
{
for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
{
DomainArray.Add(myresponse[i].EmailAddressSuffixes[j]);
}
}
Related
I am new to c# and I am trying to print parts of an integer array sorted by multiple threads. For that I use bubble sort inside my function mySort which takes part of the array as a parameter and stores it to a list that I want to display afterwards. The division of the original array is in a loop where I also start the threads. The problem is that the resulting parts are all the same although I pass the different parts of the array to a function to be executed on a thread. The resulting parts includes just numbers of the last part of the original sorted array. I would be glad for any help.
int[] myArray = new int[100];
for (int i = 0; i < myArray.Length; i++)
{
myArray[i] = r.Next(0, 100);
}
parts = new List<int[]>();
Thread[] t = new Thread[5];
int[] tmp = new int[myArray.Length/5];
int k = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < tmp.Length; j++)
{
tmp[j] = myArray[k];
k++;
}
Thread myT = new Thread(() => mySort(tmp));
t[i] = myT;
t[i].Start();
}
Function for sorting
static void mySort(int [] arr)
{
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = 0; j < arr.Length - i - 1; j++)
{
if (arr[j + 1] < arr[j])
{
int tmp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = tmp;
}
}
}
parts.Add(arr);
}
EDIT:
By declaring tmp array inside for loop the problem is solved.
for (int i = 0; i < 5; i++)
{
int[] tmp = new int[myArray.Length/5];
for (int j = 0; j < tmp.Length; j++)
{
tmp[j] = myArray[k];
k++;
}
Thread myT = new Thread(() => mySort(tmp));
t[i] = myT;
t[i].Start();
}
I initialize a list of list:
List<List<double>> List = new List<List<double>>();
for(int k = 0; k < 4; k++)
{
liste_moyenne.Add(new List<double>(5));
}
I want to insert the values of a list (this list is generated by another function) like this:
for that I did this piece of code:
for (int i = 0; i < List.Count; i++)
{
for (int j = 0; j < myList.Count; j++)
{
List[i].Add(AddSmthToWork(myList[j]))
}
}
How can I do that?
Here's an example using the AddRange method:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var listOfList = new List<List<double>>();
for (var i = 0; i < 4; i++)
{
listOfList.Add(new List<double>(5));
}
foreach (var item in listOfList)
{
item.AddRange(GetList(5));
}
for(var i = 0; i < listOfList.Count; i++)
{
for (var j = 0; j < listOfList[i].Count; j++)
{
Console.WriteLine($"listOfList[{i}][{j}] = {listOfList[i][j]}");
}
}
}
private static IList<double> GetList(int maxCapacity)
{
var newList = new List<double>(maxCapacity);
for (var i = 0; i < maxCapacity; i++)
{
newList.Add(i);
}
return newList;
}
}
With the output:
listOfList[0][0] = 0
listOfList[0][1] = 1
listOfList[0][2] = 2
listOfList[0][3] = 3
listOfList[0][4] = 4
listOfList[1][0] = 0
listOfList[1][1] = 1
listOfList[1][2] = 2
listOfList[1][3] = 3
listOfList[1][4] = 4
listOfList[2][0] = 0
listOfList[2][1] = 1
listOfList[2][2] = 2
listOfList[2][3] = 3
listOfList[2][4] = 4
listOfList[3][0] = 0
listOfList[3][1] = 1
listOfList[3][2] = 2
listOfList[3][3] = 3
listOfList[3][4] = 4
See this tutorial
List<List<double>> nestedList = new List<List<double>>();
for (int i = 0; i < 4/*myCount*/; i++)
{
List<double> sublist = new List<double>();
for (int j = 0; j < 4/*myCount*/; j++)
{
sublist.Add(/*myValue*/)
}
nestedList.Add(sublist);
}
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
}
}
});
The code I tried so far below:
private static List<List<List<int>>> threeDArrayToThreeDList(int [,,] letters) {
// 3d-array to 3d-list
List<List<List<int>>> letterslist = new List<List<List<int>>>();
List<List<int>> sublist = new List<List<int>> ();
List<int> subsublist = new List<int> ();
for (int i = 0; i < 2; i++) {
letterslist.Add (sublist);
for (int j = 0; j < 2; j++) {
letterslist[i].Add (subsublist);
for (int k = 0; k < 2; k++) {
Console.WriteLine (letterslist [i][j][k]); // Element not found
Console.WriteLine (letters [i,j,k]);
letterslist [i] [j] [k] = letters [i,j,k];
}
}
}
return letterslist;
}
Why letterslist [i][j][k] isn't found?
Your code is wrong. You need to create a list for each "index". You're code only creates 3 lists altogether.
Here's how it should work:
private static List<List<List<int>>> threeDArrayToThreeDList(int [,,] letters) {
// 3d-array to 3d-list
List<List<List<int>>> letterslist = new List<List<List<int>>>();
for (int i = 0; i < 2; i++) {
letterslist.Add (new List<List<int>> ());
for (int j = 0; j < 2; j++) {
letterslist[i].Add (new List<int> ());
for (int k = 0; k < 2; k++) {
Console.WriteLine (letters [i,j,k]);
letterslist [i] [j].Add(letters [i,j,k]);
}
}
}
return letterslist;
}
This because the letterslist [i][j] list has no elements in it.
Add an element to it, and it will get you through the line that causes the exception.
Change the code in the innermost loop as follows:
for (int k = 0; k < 2; k++) {
letterslist[i][j].Add (letters[i, j, k]);
Console.WriteLine(letterslist[i][j][k]); // Should work fine
Console.WriteLine(letters[i, j, k]);
}
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.