-12
 std::vector<float> a {-0.2, 2.0, -0.9, 2.4, 22.0}

How to take absolute value of all vector elements?

The result should be

{0.2, 2.0, 0.9, 2.4, 22.0}
cigien
  • 57,834
  • 11
  • 73
  • 112
KBabuin
  • 89
  • 1
  • 1
  • 6
  • 2
    Loop over all elements, call `abs` on them? – anderas Aug 10 '15 at 08:50
  • 2
    Try using google : top of the list - http://math.stackexchange.com/questions/707945/how-to-find-the-absolute-value-of-a-vector – PaulF Aug 10 '15 at 08:52
  • The requirements of this question are clear, and the scope is sufficiently narrow. I'm voting to reopen. – cigien May 08 '21 at 18:31

4 Answers4

7

this code will help you, loop the vector and apply abs (function to find absolute value )

   for(unsigned int i = 0; i < numbers.size(); i++)
    {
        if(numbers[i] < 0)numbers[i] *= -1; //make positive.    _OR_   use numbers[i] = abs(numbers[i]);
        std::cout<<numbers[i]<<std::endl;
    }
Akhil V Suku
  • 870
  • 2
  • 13
  • 34
  • 2
    or with for range of C++11: `for (auto& f : numbers) { f = f < 0 ? -f : f;}` – Jarod42 Aug 10 '15 at 09:05
  • std::vector absvalues(a.size()); std::transform( a.begin(), a.end(), absvalues.begin(), [](double coeff) { return std::abs(coeff); } ); – Markus Nov 12 '20 at 02:33
4

Use the formula |a| = sqrt(sum(ai*ai)):

float value = 0.0;
for(int i = 0; i < a.size(); i++) value += a[i] * a[i];
value = sqrt(value);
Community
  • 1
  • 1
John Bupit
  • 10,406
  • 8
  • 39
  • 75
  • My math classes are some while ago now, but shoudn't it be |a| = sqrt(sum(abs(ai)*abs(aj))). Otherwise you may run into a exception when the sqrt of a negative value is taken. (Plus the result may be wrong) – Hermann Klecker Aug 10 '15 at 09:02
  • It seems that it is not what OP wants. You compute a distance whereas OP wants to modify its vector to have positive float. – Jarod42 Aug 10 '15 at 09:03
  • `ai*ai` = `abs(ai)*abs(ai)`, isn't it? – John Bupit Aug 10 '15 at 09:03
  • @Jarod42 Oops. The title confused me a bit. It said [*absolute value of vector*](https://en.wikipedia.org/wiki/Absolute_value#Vector_spaces), which led to this answer. – John Bupit Aug 10 '15 at 09:08
  • The expression `a[i] * a[i++]` has undefined behavior. http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points – Benjamin Lindley Aug 10 '15 at 09:10
  • @BenjaminLindley Did not know that! Thanks. But a for loop like `for(int i = 0; i < 42; a[i++] = 0);` should be valid, shouldn't it? – John Bupit Aug 10 '15 at 09:20
  • 1
    Yeah, that's perfectly fine. The problem is when you access the same scalar value two or more times in different parts of the same expression, and one or more of those accesses is modification. (the actual rule is not quite that strict, but it's a good rule to follow imo, and it avoids this particular brand of UB). – Benjamin Lindley Aug 10 '15 at 09:27
3

You can use following code.

#include <iostream>
#include <string>
#include <cstring>
#include <vector>

using namespace std;

void v_abs( vector <int>  &x, double p[]) 
{ 
    int i=0; 


    while( p[i] < x.size () ) 
    { 
        if ( p[i] < 0 )
        { 
            p[i] *= -1 ; 
         } 
             i ++; 
    }
}

int main() {
    cout << v_abs << endl;
    system("pause");
    return 0;
}
DhruvJoshi
  • 17,041
  • 6
  • 41
  • 60
1

You create a function that receives as input the vector and returns another vector:

const std::vector< float >& absValues(std::vector< float >& vecIn)
{
   for(float &val : vecIn)
   {
      if (val < 0)
      {
          val *= -1;
      }
   }

   return vecIn;
}

And if you want the sqrt of the sum of all elements of the vector, then you should do it as John Bupit said (IMHO is nice)

Community
  • 1
  • 1
sop
  • 3,445
  • 8
  • 41
  • 84