943,590 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1036
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 20th, 2008
0

ordering arrays

Expand Post »
i have an array

weights[16][60]

this being 16 rows of 60 elements in each one

i have another

error[16]

which co incides with each with the 16 rows, this is the error from each row.

is there a way i can put each of the 16 in order in the array and then mimic that into the weights array so that all of those are like the ones in the error array?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
666kennedy is offline Offline
63 posts
since Mar 2008
Nov 20th, 2008
0

Re: ordering arrays

I'm afraid I'm not getting what you're trying to say. Maybe it's too late for this... :-)
Are you trying to put the content of one array into the other or am I getting you wrong? Please elaborate
Reputation Points: 26
Solved Threads: 9
Junior Poster in Training
brechtjah is offline Offline
92 posts
since Nov 2008
Nov 20th, 2008
0

Re: ordering arrays

You want to sort the error array and keep the relationship with the rows in weights array ???

One way is to use another array that contains indices into both error and weights array. When you sort error array actually move the rows in this third array. Use the third array as indirect access to the other arrays.
C++ Syntax (Toggle Plain Text)
  1. indices[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  2. // sort errors array
  3. for(int i = 0; i < 15; i++)
  4. {
  5. for(int j= i+1; j < 16; j++)
  6. {
  7. if( errors[ indices[i] ] < errors[ indices[j] ] )
  8. {
  9. int temp = indices[i];
  10. indices[i] = indices[j];
  11. indices[j] = temp;
  12. }
  13. }
  14. }

Now to reference weights: assume variable X is an integer that contains the value from errors index
weights[ indices[x] ][0];

Or, if that is a little too complex for you, just sort both errors and weights array at the same time. When you exchange elements of errors array also exchange all columns in the corresponding rows of weights.
Last edited by Ancient Dragon; Nov 20th, 2008 at 9:04 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Nov 20th, 2008
0

Re: ordering arrays

You want to sort the error array and keep the relationship with the rows in weights array ???
Oh, now I get it,
maybe another way to do this is keeping an ID of every array row.
And while you order one array, locate the ID in the other and move that one too.
Or compare the IDs after the ordering is done, and move them according to that.

You'll need to add an extra dimension to your array for that though...
Hope this helps
Reputation Points: 26
Solved Threads: 9
Junior Poster in Training
brechtjah is offline Offline
92 posts
since Nov 2008
Nov 20th, 2008
0

Re: ordering arrays

sorry i have been looking at code solid for the last few days so my brain is wasted!

right the 16 rows in the weights array all have a fitness rating or error, which is stored in the array error

here is the code

C++ Syntax (Toggle Plain Text)
  1. for(int i = 0; i < 16; i++)
  2. {
  3. for(int j = 0; j < 60; j++)
  4. {
  5. weights[i][j] = -1 + rand() / ( RAND_MAX / ( 1 + 1 ) + 0.1 );
  6. }
  7. }
  8.  
  9.  
  10. //---- test strings for fitness --------------------------
  11.  
  12.  
  13. for(int i = 0; i < 16; i++)
  14. {
  15. toterr = 0;
  16. for (pat = 1; pat < 4; pat++)
  17. {
  18. patpixselect();
  19.  
  20. cout << endl << "pattern: " << pat << endl;
  21.  
  22. cout << endl << patpix[0] << endl;
  23. getch();
  24. OUTA = 1/(1 + pow(e, -((weights[i][0] * patpix[0]) + (weights[i][1] * patpix[1])
  25. + (weights[i][2] * patpix[2] ) + (weights[i][3] * patpix[3]) + (weights[i][4] * patpix[4])
  26. + ( weights[i][5] * patpix[5]) + (weights[i][6] * patpix[6]) + (weights[i][7] * patpix[7])
  27. + (weights[i][8] * patpix[8]))));
  28. cout << "OUTA " << OUTA << endl;
  29.  
  30. OUTB = 1/(1 + pow(e, -((weights[i][9] * patpix[0]) + (weights[i][10] * patpix[1])
  31. + (weights[i][11] * patpix[2] ) + (weights[i][12] * patpix[3]) + (weights[i][13] * patpix[4])
  32. + ( weights[i][14] * patpix[5]) + (weights[i][15] * patpix[6]) + (weights[i][16] * patpix[7])
  33. + (weights[i][17] * patpix[8]))));
  34. cout << "OUTB " << OUTB << endl;
  35.  
  36. OUTC = 1/(1 + pow(e, -((weights[i][18] * patpix[0]) + (weights[i][19] * patpix[1])
  37. + (weights[i][20] * patpix[2] ) + (weights[i][21] * patpix[3]) + (weights[i][22] * patpix[4])
  38. + ( weights[i][23] * patpix[5]) + (weights[i][24] * patpix[6]) + (weights[i][25] * patpix[7])
  39. + (weights[i][26] * patpix[8]))));
  40. cout << "OUTC " << OUTC << endl;
  41.  
  42. OUTD = 1/(1 + pow(e, -((weights[i][27] * patpix[0]) + (weights[i][28] * patpix[1])
  43. + (weights[i][29] * patpix[2] ) + (weights[i][30] * patpix[3]) + (weights[i][31] * patpix[4])
  44. + ( weights[i][32] * patpix[5]) + (weights[i][33] * patpix[6]) + (weights[i][34] * patpix[7])
  45. + (weights[i][35] * patpix[8]))));
  46. cout << "OUTD " << OUTD << endl;
  47.  
  48. OUTE = 1/(1 + pow(e, -((weights[i][36] * patpix[0]) + (weights[i][37] * patpix[1])
  49. + (weights[i][38] * patpix[2] ) + (weights[i][39] * patpix[3]) + (weights[i][40] * patpix[4])
  50. + ( weights[i][41] * patpix[5]) + (weights[i][42] * patpix[6]) + (weights[i][43] * patpix[7])
  51. + (weights[i][44] * patpix[8]))));
  52. cout << "OUTE " << OUTE << endl;
  53.  
  54. OUTX = 1/(1 + pow(e, -((weights[i][45] * OUTA) + (weights[i][46] * OUTB)
  55. + (weights[i][47] * OUTC ) + (weights[i][48] * OUTD) + (weights[i][49] * OUTE))));
  56. cout << "OUTX " << OUTX << endl;
  57.  
  58. OUTY = 1/(1 + pow(e, -((weights[i][50] * OUTA) + (weights[i][51] * OUTB)
  59. + (weights[i][52] * OUTC ) + (weights[i][53] * OUTD) + (weights[i][54] * OUTE))));
  60. cout << "OUTY " << OUTY << endl;
  61.  
  62. OUTZ = 1/(1 + pow(e, -((weights[i][55] * OUTA) + (weights[i][56] * OUTB)
  63. + (weights[i][57] * OUTC ) + (weights[i][58] * OUTD) + (weights[i][59] * OUTE))));
  64. cout << "OUTZ " << OUTZ << endl;
  65.  
  66. // ------------- calculate errors -(fitness)--------------------------------------------------
  67.  
  68. cout << endl << endl << "target 1 is " << target1 << endl
  69. << "target 2 is " << target2 << endl << "target3 is " << target3;
  70.  
  71.  
  72.  
  73. ERRX = (OUTX)*(1 - OUTX)*(target1 - OUTX ); // error of out put x
  74. ERRX = pow(ERRX,2);
  75. ERRX = sqrt(ERRX);
  76.  
  77. ERRY = (OUTY)*(1 - OUTY)*(target2 - OUTY ); // err of output Y
  78. ERRY = pow(ERRY,2);
  79. ERRY = sqrt(ERRY);
  80.  
  81. ERRZ = (OUTZ)*(1 - OUTZ)*(target3 - OUTZ ); // err of output z
  82. ERRZ = pow(ERRZ,2);
  83. ERRZ = sqrt(ERRZ);
  84.  
  85. err = ( ERRX + ERRY + ERRZ ); // total error of 1 pattern for 1 string
  86. cout << endl << "total error for 1 pattern for 1 string " << err << endl;
  87.  
  88.  
  89. toterr += err; // --- error for all patterns for 1 string
  90. cout << endl << "total error for all patterns for 1 string " << toterr << endl;
  91. }
  92.  
  93. error[i] = toterr; // put each error for each string in array to match with each string.
  94. cout << endl << "total error for string " << i << " is " << error[i] << endl ;
  95. }
  96.  
  97. getch();
  98. return 0;

basically there will be 16 numbers in the array known as error, and i need a way of putting them into order of lowest to highest based on the error.

if it helps im training a neural network to recognise 3 patterns using a genetic algorithm, so the part im on right now is sorting out the best half of the strings ( or the best half of the 16 in the array weights) so i can pair them up, swap elements around and create another 8 strings to replace the weakest half of the set. which i will need help with too!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
666kennedy is offline Offline
63 posts
since Mar 2008
Nov 20th, 2008
0

Re: ordering arrays

Quote ...

indices[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
// sort errors array
for(int i = 0; i < 15; i++)
{
for(int j= i+1; j < 16; j++)
{
if( errors[ indices[i] ] < errors[ indices[j] ] )
{
int temp = indices[i];
indices[i] = indices[j];
indices[j] = temp;
}
}
}
could you please explain how this works, like im more dumbed down, i can kinda see how it would work, but if u could just run through it in words or something that would be grately appreciated
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
666kennedy is offline Offline
63 posts
since Mar 2008
Nov 20th, 2008
0

Re: ordering arrays

indices is used to indirectly reference the elements of the other two arrays. IMO the best way to understand the concept is to write a small program and test it out. Sort the array then print out the values of the errors array.
C++ Syntax (Toggle Plain Text)
  1. // assume array has been sorted per my previous post
  2. //
  3. // now just simply display the values of the errors array
  4. for(int i = 0; i < 16; i++)
  5. cout << errors[ indices[i] ] << "\n";
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Nov 20th, 2008
0

Re: ordering arrays

I think I understand your first post now and the logic would go something like this.
C++ Syntax (Toggle Plain Text)
  1. I have a multidimensional array of 16 rows and 60 columns per row called 'weights'.
  2. I have an array of 16 rows called 'error'.
  3. I assume the first element in the 'error' array has the lowest error and I'll call that element 'lowest' (no variable)
  4. While the number of the row is less than 16
  5. if the next element of 'error is less than 'lowest'
  6. Switch values of 'lowest' and current element
  7. While X is 0 and X is less than 60
  8. Switch values of rows of same index as 'lowest' and the current element in the 'weights' array
  9. X increases by 1
Something like that

If you need more help say so and I'll try to help if I can.

Good luck
Last edited by emotionalone; Nov 20th, 2008 at 10:47 pm.
Reputation Points: 10
Solved Threads: 4
Light Poster
emotionalone is offline Offline
33 posts
since Oct 2008
Nov 21st, 2008
0

Re: ordering arrays

cheers that helps a bit,

basically what your doing is calling one of the elements in error the lowest until something else comes along which is lower, so basically adding on a number to each one thats lower and adding one for each thats bigger kind of thing?

i was thinking of adding a few more dimensions to the arrays

with error i would add another dimension which would put a number 0 to 15 along side the error depending on the position it comes

i already have 0 to 15 as row numbers, i could add another dimension to keep new position in, which would co inside with the error arrays new number order.

but apart from that making it easy for me to read i need to be able to swap them all around puting 0 (or number1) at the top and the worst at the bottom.

i take it i would need a temporary place to store the file being moved until a place in the array has become available?

i kind of know the logic towards it, its just trying to put into code in a way i understand it is the hard part
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
666kennedy is offline Offline
63 posts
since Mar 2008
Nov 21st, 2008
0

Re: ordering arrays

Do you want to go from the first part of this image to the second part?
Assuming in the first part the values are in a random order and blocks of the same color represent linked data.

http://img300.imageshack.us/my.php?image=errorjm8.jpg

What you do is, again, assume the very first is the lowest and then compare the element of that index to every single other element of the array, in this case 'error' and if there's a lowest value you switch values using an auxiliary variable, yes. In addition to that right after doing that switch and before doing the next comparison you switch the values of all your columns for the 2 rows in 'weights' involved. But, you keep comparing the element of the same index until you've reach the end. And you do this for all elements of 'error'.
So if you first compare element in error of index 0 with index 1, you then compare index 0 with index 2 and even if index 2 is lowest and you do the switch the next comparison will be index 0 and index 3 followed by index 0 and index 4. If 'error' were to have only 5 rows, you would stop the comparisons with index 0. the next comparison would be index 1 and index 2, and so on. The last comparison will have to be index 3 and index 4.

I hope this helps.
Reputation Points: 10
Solved Threads: 4
Light Poster
emotionalone is offline Offline
33 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: please help, this is what i have done so far
Next Thread in C++ Forum Timeline: converting bool[8] to char





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC