c++ - Finding smallest values of given vectors -


how can find smallest value of each column in given set of vectors efficiently ?

for example, consider following program:

#include <iostream> #include <vector> #include <iterator> #include <cstdlib> using namespace std;   typedef vector<double> v_t;  int main(){  v_t v1,v2,v3;  (int = 1; i<10; i++){  v1.push_back(rand()%10);  v2.push_back(rand()%10);  v3.push_back(rand()%10); }  copy(v1.begin(), v1.end(), ostream_iterator<double>(cout, " "));   cout << endl; copy(v2.begin(), v2.end(), ostream_iterator<double>(cout, " "));   cout << endl; copy(v3.begin(), v3.end(), ostream_iterator<double>(cout, " "));   cout << endl; } 

let output be

3 5 6 1 0 6 2 8 2  6 3 2 2 9 0 6 7 0  7 5 9 7 3 6 1 9 2  

in program want find smallest value of every column (of 3 given vectors) , put vector. in program want define vector v_t vfinal have values :

3 3 2 1 0 0 1 7 0 

is there efficient way ? mention efficient because program may have find smallest values among large number of vectors. thank you.

update:

i'm trying use used in 1 of previous programs

int count = std::inner_product(a, a+5, b, 0, std::plus<int>(), std::less<int>()); 

this counts number of minimum elements between 2 arrays , b. wouldn't efficient enough if loop through , use similar kind of function find minimal values ? i'm not claiming can done or not. it's idea may improved upon don't know how.

you can use std::transform this. loops still there, they're hidden inside algorithm. each additional vector process call std::transform.

this example problem in 2 linear passes.

typedef std::vector<double> v_t;  int main() {     v_t v1,v2,v3,vfinal(9); // note: vfinal sized accept results      (int = 1; < 10; ++i) {         v1.push_back(rand() % 10);         v2.push_back(rand() % 10);         v3.push_back(rand() % 10);     }      std::transform(v1.begin(), v1.end(), v2.begin(), vfinal.begin(), std::min<double>);     std::transform(v3.begin(), v3.end(), vfinal.begin(), vfinal.begin(), std::min<double>); } 

note: works in msvc++ 2010. had provide min functor gcc 4.3.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -