當我運行腳本時,出現「索引在數組邊界之外」錯誤,線路爲float[] i_f = b[itemid];
。「索引超出了數組邊界」乘法2個矩陣的錯誤
BTW,我改變
for (int y = 0; y <= 114360000; y++)
到for (int y = 0; y < 114360000; y++)
也
for (int x = 0; x <= 8939500; x++)
到for (int x = 0; x < 8939500; x++)
,但它再次顯示了同樣的錯誤。
供參考:該代碼有兩個部分:即得到2點矩陣100倍的值和它們相乘(矩陣乘法),其正常工作
第一部分。
重複第一部分1143600次的第二部分(運行此部分時發生錯誤)。
namespace function
{
public partial class Form1 : Form
{
//Hashtable hashtable = new Hashtable();
float userscore, itemscore,result;
string lineitem, lineuser;
//float[][] a = new float[89395][100];
//float[][] b = new float[1143600][100];
float[][] a = Enumerable.Range(0, 89395).Select(i => new float[100]).ToArray();
float[][] b = Enumerable.Range(0, 1143600).Select(j => new float[100]).ToArray();
//float[,] c = new float[89395, 100];
StreamReader fileitem = new StreamReader("c:\\p.txt");
StreamReader fileuser = new StreamReader("c:\\L.txt");
public Form1()
{
InitializeComponent();
for (int x = 0; x <= 8939500; x++)
{
lineuser = fileuser.ReadLine();
if (!string.IsNullOrEmpty(lineuser))
{
string[] values = lineuser.Split(' ');
int userid, factoriduser;
foreach (string value in values)
{
userid = Convert.ToInt32(values[0]);
factoriduser = Convert.ToInt32(values[1]);
userscore = Convert.ToSingle(values[2]);
a[userid][factoriduser] = userscore;
}
}
}
for (int y = 0; y <= 114360000; y++)
{
lineitem = fileitem.ReadLine();
if (!string.IsNullOrEmpty(lineitem))
{
string[] valuesi = lineitem.Split(' ');
int itemid, factoriditem;
foreach (string value in valuesi)
{
itemid = Convert.ToInt32(valuesi[0]);
factoriditem = Convert.ToInt32(valuesi[1]);
itemscore = Convert.ToSingle(valuesi[2]);
b[itemid][factoriditem] = itemscore;
}
}
}
}
public float dotproduct(int userid, int itemid)
{
//get the score of 100 from user and item to dotproduct
//get userid and itemid element from array a and b
float[] u_f = a[userid];
float[] i_f = b[itemid]; //<-----error happens
for (int i = 0; i < u_f.Length; i++)
{
// result += u_f[userid] * i_f[itemid];
result += u_f[i] * i_f[i];
}
return result;
}
private void btn_recomm_Click(object sender, EventArgs e)
{
// if (txtbx_id.Text ==null)
if (String.IsNullOrEmpty(txtbx_id.Text))
{
MessageBox.Show("please insert user id");
}
//if (txtbx_id.Text != null && txtbx_itemid == null)
if (!String.IsNullOrEmpty(txtbx_id.Text) && String.IsNullOrEmpty(txtbx_itemid.Text))
{
int sc = Convert.ToInt32(txtbx_id.Text);
if (sc >= 0 && sc <= 89395)
{
//for (int z = 0; z <= 1143600; z++)
//{
// dotproduct(sc, z);
// hashtable.Add(z, result);
//}
//SortedDictionary<int, float> dict = new SortedDictionary<int, float>(hashtable);
//foreach (int key in dict)
//{
// System.Console.WriteLine(String.Format("{0}: {1}", key, dict[key]));
//}
var results = new List<float>(1143600);
for (int z = 0; z <= 1143600; z++)
{
results.Add(dotproduct(sc, z));
}
foreach (var resultwithindex in results.Select((r, index) => new { result = r, Index = index }).OrderByDescending(r => r.result).Take(10))
{
System.Console.WriteLine(string.Format("{0}: {1}", resultwithindex.Index, resultwithindex.result));
}
}
}
//if (txtbx_id!=null && txtbx_itemid!=null)
if (!String.IsNullOrEmpty(txtbx_id.Text) && !String.IsNullOrEmpty(txtbx_itemid.Text))
{
int uid = Convert.ToInt32(txtbx_id.Text);
// int uid;
// if (!Int32.TryParse(txtbx_id.Text, out uid))
// {
// MessageBox.Show("Try again");
// }
int iid = Convert.ToInt32(txtbx_itemid.Text);
// int iid;
//if (!Int32.TryParse(txtbx_id.Text, out iid))
//{
//MessageBox.Show("Try again");
//}
//{
if (uid >= 0 && uid <= 89395 && iid >= 0 && iid <= 1143600)
{
dotproduct(uid, iid);
MessageBox.Show("The Score of item id " + iid + " is " + result);
}
}
}
請簡化代碼到一個易於遵循的例子,說明了一個問題,就是太多的代碼,而調試器遵循徹底。 –