how to get index of minimum of one array
-
dear all how to get index of minimum of one array? the following is my code...but however the outcome is wrong after compiled...actually the error is corrNum. fitness[10] is an 10 elements array,first i would like to calculate minimum value of fitness, and then i need to use its index of this minimum value of array,but result is wrong.anyone can help me? thanks a lot. for (int i=0;i<10;i++) { double minValue = fitness[9]; if (fitness[i] <= minValue) { minValue = fitness[i]; for (int j=0;j<150;j++) { value = h[i][j] - irisIndNew[j]; if (value == 0) corrNum = corrNum + 1; } } }
Li Zhiyuan
-
dear all how to get index of minimum of one array? the following is my code...but however the outcome is wrong after compiled...actually the error is corrNum. fitness[10] is an 10 elements array,first i would like to calculate minimum value of fitness, and then i need to use its index of this minimum value of array,but result is wrong.anyone can help me? thanks a lot. for (int i=0;i<10;i++) { double minValue = fitness[9]; if (fitness[i] <= minValue) { minValue = fitness[i]; for (int j=0;j<150;j++) { value = h[i][j] - irisIndNew[j]; if (value == 0) corrNum = corrNum + 1; } } }
Li Zhiyuan
Try moving the line "double minValue = fitness[9];" outside of the for (int i...) loop. It's getting assigned to fitness[9] every time through the loop. double minValue = fitness[9]; for (int i=0;i<10;i++) { if (fitness[i] <= minValue) { minValue = fitness[i]; for (int j=0;j<150;j++) { value = h[i][j] - irisIndNew[j]; if (value == 0) corrNum = corrNum + 1; } } }
- S 50 cups of coffee and you know it's on!
-
dear all how to get index of minimum of one array? the following is my code...but however the outcome is wrong after compiled...actually the error is corrNum. fitness[10] is an 10 elements array,first i would like to calculate minimum value of fitness, and then i need to use its index of this minimum value of array,but result is wrong.anyone can help me? thanks a lot. for (int i=0;i<10;i++) { double minValue = fitness[9]; if (fitness[i] <= minValue) { minValue = fitness[i]; for (int j=0;j<150;j++) { value = h[i][j] - irisIndNew[j]; if (value == 0) corrNum = corrNum + 1; } } }
Li Zhiyuan
#include <algorithm> using namespace std; int main() { double myArray[10] = {2.12,432.12,543.1,0.32,0.65,12.,54.2,542.5,100,200}; double* start = myArray; double* minimumPosition = min_element(myArray, myArray + 10); double minValue = *minimumPosition; int indexOfMinimum = minimumPosition - start; return 0; }
-
Try moving the line "double minValue = fitness[9];" outside of the for (int i...) loop. It's getting assigned to fitness[9] every time through the loop. double minValue = fitness[9]; for (int i=0;i<10;i++) { if (fitness[i] <= minValue) { minValue = fitness[i]; for (int j=0;j<150;j++) { value = h[i][j] - irisIndNew[j]; if (value == 0) corrNum = corrNum + 1; } } }
- S 50 cups of coffee and you know it's on!
thanks friend. could you help me to check the following code? why i couldn't assign value of p[gbest_Ind][][] to gbest1[gbest_Ind][][], thanks a lot minValue = fitness[0]; for (int count = 0;count<10;count++) { if (fitness[count] < minValue ) { minValue = fitness[count]; gbest_Ind = count; } } for (int j = 0; j<3; j++) for (int k = 0; k<4; k++) { gbest1[gbest_Ind][j][k]=p[gbest_Ind][j][k]; }
Li Zhiyuan