| | |
ordering arrays
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 63
Reputation:
Solved Threads: 0
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?
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?
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.
Now to reference weights: assume variable X is an integer that contains the value from errors index
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.
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)
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; } } }
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
You want to sort the error array and keep the relationship with the rows in weights array ???
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
•
•
Join Date: Mar 2008
Posts: 63
Reputation:
Solved Threads: 0
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
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!
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)
for(int i = 0; i < 16; i++) { for(int j = 0; j < 60; j++) { weights[i][j] = -1 + rand() / ( RAND_MAX / ( 1 + 1 ) + 0.1 ); } } //---- test strings for fitness -------------------------- for(int i = 0; i < 16; i++) { toterr = 0; for (pat = 1; pat < 4; pat++) { patpixselect(); cout << endl << "pattern: " << pat << endl; cout << endl << patpix[0] << endl; getch(); OUTA = 1/(1 + pow(e, -((weights[i][0] * patpix[0]) + (weights[i][1] * patpix[1]) + (weights[i][2] * patpix[2] ) + (weights[i][3] * patpix[3]) + (weights[i][4] * patpix[4]) + ( weights[i][5] * patpix[5]) + (weights[i][6] * patpix[6]) + (weights[i][7] * patpix[7]) + (weights[i][8] * patpix[8])))); cout << "OUTA " << OUTA << endl; OUTB = 1/(1 + pow(e, -((weights[i][9] * patpix[0]) + (weights[i][10] * patpix[1]) + (weights[i][11] * patpix[2] ) + (weights[i][12] * patpix[3]) + (weights[i][13] * patpix[4]) + ( weights[i][14] * patpix[5]) + (weights[i][15] * patpix[6]) + (weights[i][16] * patpix[7]) + (weights[i][17] * patpix[8])))); cout << "OUTB " << OUTB << endl; OUTC = 1/(1 + pow(e, -((weights[i][18] * patpix[0]) + (weights[i][19] * patpix[1]) + (weights[i][20] * patpix[2] ) + (weights[i][21] * patpix[3]) + (weights[i][22] * patpix[4]) + ( weights[i][23] * patpix[5]) + (weights[i][24] * patpix[6]) + (weights[i][25] * patpix[7]) + (weights[i][26] * patpix[8])))); cout << "OUTC " << OUTC << endl; OUTD = 1/(1 + pow(e, -((weights[i][27] * patpix[0]) + (weights[i][28] * patpix[1]) + (weights[i][29] * patpix[2] ) + (weights[i][30] * patpix[3]) + (weights[i][31] * patpix[4]) + ( weights[i][32] * patpix[5]) + (weights[i][33] * patpix[6]) + (weights[i][34] * patpix[7]) + (weights[i][35] * patpix[8])))); cout << "OUTD " << OUTD << endl; OUTE = 1/(1 + pow(e, -((weights[i][36] * patpix[0]) + (weights[i][37] * patpix[1]) + (weights[i][38] * patpix[2] ) + (weights[i][39] * patpix[3]) + (weights[i][40] * patpix[4]) + ( weights[i][41] * patpix[5]) + (weights[i][42] * patpix[6]) + (weights[i][43] * patpix[7]) + (weights[i][44] * patpix[8])))); cout << "OUTE " << OUTE << endl; OUTX = 1/(1 + pow(e, -((weights[i][45] * OUTA) + (weights[i][46] * OUTB) + (weights[i][47] * OUTC ) + (weights[i][48] * OUTD) + (weights[i][49] * OUTE)))); cout << "OUTX " << OUTX << endl; OUTY = 1/(1 + pow(e, -((weights[i][50] * OUTA) + (weights[i][51] * OUTB) + (weights[i][52] * OUTC ) + (weights[i][53] * OUTD) + (weights[i][54] * OUTE)))); cout << "OUTY " << OUTY << endl; OUTZ = 1/(1 + pow(e, -((weights[i][55] * OUTA) + (weights[i][56] * OUTB) + (weights[i][57] * OUTC ) + (weights[i][58] * OUTD) + (weights[i][59] * OUTE)))); cout << "OUTZ " << OUTZ << endl; // ------------- calculate errors -(fitness)-------------------------------------------------- cout << endl << endl << "target 1 is " << target1 << endl << "target 2 is " << target2 << endl << "target3 is " << target3; ERRX = (OUTX)*(1 - OUTX)*(target1 - OUTX ); // error of out put x ERRX = pow(ERRX,2); ERRX = sqrt(ERRX); ERRY = (OUTY)*(1 - OUTY)*(target2 - OUTY ); // err of output Y ERRY = pow(ERRY,2); ERRY = sqrt(ERRY); ERRZ = (OUTZ)*(1 - OUTZ)*(target3 - OUTZ ); // err of output z ERRZ = pow(ERRZ,2); ERRZ = sqrt(ERRZ); err = ( ERRX + ERRY + ERRZ ); // total error of 1 pattern for 1 string cout << endl << "total error for 1 pattern for 1 string " << err << endl; toterr += err; // --- error for all patterns for 1 string cout << endl << "total error for all patterns for 1 string " << toterr << endl; } error[i] = toterr; // put each error for each string in array to match with each string. cout << endl << "total error for string " << i << " is " << error[i] << endl ; } getch(); 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!
•
•
Join Date: Mar 2008
Posts: 63
Reputation:
Solved Threads: 0
•
•
•
•
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;
}
}
}
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)
// assume array has been sorted per my previous post // // now just simply display the values of the errors array for(int i = 0; i < 16; i++) cout << errors[ indices[i] ] << "\n";
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Oct 2008
Posts: 32
Reputation:
Solved Threads: 4
I think I understand your first post now and the logic would go something like this.
Something like that
If you need more help say so and I'll try to help if I can.
Good luck
C++ Syntax (Toggle Plain Text)
I have a multidimensional array of 16 rows and 60 columns per row called 'weights'. I have an array of 16 rows called 'error'. I assume the first element in the 'error' array has the lowest error and I'll call that element 'lowest' (no variable) While the number of the row is less than 16 if the next element of 'error is less than 'lowest' Switch values of 'lowest' and current element While X is 0 and X is less than 60 Switch values of rows of same index as 'lowest' and the current element in the 'weights' array X increases by 1
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.
•
•
Join Date: Mar 2008
Posts: 63
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Oct 2008
Posts: 32
Reputation:
Solved Threads: 4
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.
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.
![]() |
Similar Threads
- Using arrays and menus... (C++)
- Ordering arrays? (C)
- Ordering an array (Java)
- Sorting arrays of pointers with function? (C)
Other Threads in the C++ Forum
- Previous Thread: please help, this is what i have done so far
- Next Thread: converting bool[8] to char
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






