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?

Recommended Answers

All 10 Replies

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

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.

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.

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 :)

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

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!

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

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.

// 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";

I think I understand your first post now and the logic would go something like this.

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

Something like that

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

Good luck

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 :(

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.

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

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.

that is exactly what i want to do. thank you so much, and for putting it into such clear language, i really appreciate it!!!

if i have more problems with this ill probably start a new thread as this one is getting a little old so watch this space!

thanks again to everyone, im loving the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.