How to get minimum value from 2D array in c# and the indices of that cell? -
i want minimum value 2 dimensional array [1024,9] ,and want position of minimum value.
hint: last column flag "if flag == 0 : check row, else : skip row"
i tried cod ,but did not me ...
float min = fill_file[0, 0]; int ind = 0; int ind2 = 0; (int = 0; < 1024; i++) { (int j = 0; j < 8; j++) { if (fill_file[i, j] < min && fill_file[i, 8] == 0) { min = fill_file[i, j]; ind2 = i; ind = j; } } }
this code based on request
int t = 0; while (t < 1024) { float min = fill_file[0, 0]; int ind = 0; int ind2 = 0; (int = 0; < 1024; i++) { (int j = 0; j < 8; j++) { if (fill_file[i, j] < min && fill_file[i, 8] == 0) { min = fill_file[i, j]; ind2 = i; ind = j; } } } machens[ind] = machens[ind] + min; fill_file[ind2, 8] = 1; (int r = 0; r < 1024; r++) { if (fill_file[r, 8] != 1) fill_file[r, ind] = fill_file[r, ind] + min; } t++; }//end while
firstly:
if happens none of rows have "use row" flag set 0 (with 0 meaning "use row"), wrong result. guess never happen data?
secondly:
if fill_file[0,8]
nonzero, still initialising min
fill_file[0,0]
though flag says shouldn't use row. in case, if fill_file[0,0]
happens less values in of rows fill_file[row,8]
0 wrong result.
i tempted initialise min
float.maxvalue
, ind
, ind2
-1 each, know if haven't been updated. , call them minrow
, mincol
.
thirdly:
there's obvious optimisation: if [row,8] nonzero, there's no point in running inner iteration row.
putting together:
float minvalue = float.maxvalue; int mincol = -1; int minrow = -1; (int row = 0; row < 1024; row++) { if (fill_file[row, 8] == 0) { (int col = 0; col < 8; col++) { if (fill_file[row, col] < minvalue) { minvalue = fill_file[row, col]; minrow = row; mincol = col; } } } } // if minrow < 0, no valid data exists. // otherwise, fill_file[minrow, mincol] contains minval
Comments
Post a Comment