The Ultimate Qt Community site
Home News Forum Wiki Contest FAQ Links

Go Back   Qt Centre Forum > Other > General Programming

General Programming Forum devoted to general, non-Qt programming issues.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12th May 2008, 14:29
mickey mickey is offline
Advanced user
 
Join Date: Jan 2006
Qt products used: Qt3
Qt platforms used: Windows
Posts: 907
Thanks: 52
Thanked 0 Times in 0 Posts
Default vector of vector and computes

Hello,
I have a matrix of int (vector of vector); the problem is that I need to compute the mean of each column (the matrix can be 500 x200); so to do this I did:
Qt Code:
  1. foreach (iter_column) {
  2.    float sum = 0;
  3.    iter_line.setFirstLine;
  4.    foreach(iter_line) {
  5.         sum += iter_line[iter_coulmn]. value;   
  6.    }
  7.    mean = sum / nOfLine;
  8. }
The problem is that I have to do "two for" one inside other; I thought If is better to do something like to tie a vector<coulum> and do:
Qt Code:
  1. class Column {
  2. public:
  3.    vector<double> _value;
  4. };
  5. vector<Column> col; //to fill col properly
  6. foreach (col) {
  7.     int sum = std::accumulate(col.begin(), col.end(), 0);
  8.     mean = sum / col.size();
  9. }
Will be the second way better/faster? Any other ways to improve?
__________________
Regards
Reply With Quote
  #2  
Old 15th May 2008, 13:47
wysota wysota is offline
Guru
 
Join Date: Jan 2006
Location: Warsaw, Poland
Qt products used: Qt3, Qt4
Qt platforms used: Unix/X11, Windows
Posts: 11,663
Thanks: 3
Thanked 1,640 Times in 1,591 Posts
Default Re: vector of vector and computes

It won't be faster, because std::accumulate will iterate the vector as well... What you can do is to calculate all columns at once and store results in a temporary vector. It won't be much faster though - you have to iterate all columns in all rows ending up with a complexity of O(n x m) anyway. The only benefit you might have is proper use of cpu cache. You could try making things faster by calculating more than one cell at once (by reading/summing all columns in one machine instruction), but you can have overflows then.

Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 06:06.

Powered by vBulletin Version 3.7.1 Copyright ©2000 - 2008, Jelsoft Enterprises Ltd., vRewrite 1.5 SEOed URLs completed by Tech Help Forum and Chalo Na.
© 2006–2008 Qt Centre - The Ultimate Qt Community site
Nokia, Qt and their respective logos are trademarks of Nokia Corporation in Finland and/or other countries worldwide.