Function to find rank
| 202 | |
| 203 | // Function to find rank |
| 204 | void rankify(std::vector<double>& A) { |
| 205 | int n = A.size(); |
| 206 | std::vector<double> R(n,0); |
| 207 | std::vector<std::tuple<double, int>> T; |
| 208 | int r = 1; |
| 209 | |
| 210 | // Create array of tuples storing value and index |
| 211 | for(int j = 0; j < n; j++) { |
| 212 | T.push_back(std::make_tuple(A[j], j)); |
| 213 | } |
| 214 | |
| 215 | // Sort tubples by data value |
| 216 | std::sort(begin(T), end(T), [](auto const &t1, auto const &t2) { |
| 217 | return get<0>(t1) < get<0>(t2); // or use a custom compare function |
| 218 | }); |
| 219 | |
| 220 | int i = 0; |
| 221 | int index, j; |
| 222 | while(i < n) { |
| 223 | j = i; |
| 224 | |
| 225 | // Get elements of same rank |
| 226 | while(j < n && std::get<0>(T[j]) == std::get<0>(T[j+1])) { |
| 227 | j++; |
| 228 | } |
| 229 | |
| 230 | int m = j - i + 1; |
| 231 | |
| 232 | for(j = 0; j < m; j++) { |
| 233 | // For each equal element use .5 |
| 234 | index = std::get<1>(T[i+j]); |
| 235 | R[index] = r + (m-1)*0.5; |
| 236 | } |
| 237 | |
| 238 | // Increment rank and index |
| 239 | r+=m; |
| 240 | i+=m; |
| 241 | } |
| 242 | |
| 243 | A.swap(R); |
| 244 | } |
| 245 | |
| 246 | void quantileNormalize(std::vector<std::vector<double>>& data) { |
| 247 | int cellCount = data.size(); |