# Vector normalisation

**URL:** <https://community.unix.com/t/vector-normalisation/307779>\
**Category:** Shell Programming and Scripting\
**Created:** [April 4, 2012, 6:42am UTC](https://community.unix.com/t/vector-normalisation/307779 "2012-04-04T06:42:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisjorg](https://community.unix.com/letter_avatar/chrisjorg/32/5_5575768a8748004e209b776fc1b2916d.png) [@chrisjorg](https://community.unix.com/u/chrisjorg)\
**Post date:** [April 4, 2012, 6:42am UTC](https://community.unix.com/t/vector-normalisation/307779/1 "2012-04-04T06:42:10Z")

</div>

In AWK

For 3 individual vectors of the form:

```nohighlight
-2.772 -9.341 -2.857
-5.140 -6.597 -1.823
-2.730 -5.615 1.159

```

I would like to write a script that parses line by line to (i) normalise, (ii) divide by the norm for \*each\* vector.

I.e.

```nohighlight
sqrt(-2.772^2 + -9.341^2 + -2.857^2)=10.154

-2.772/10.154 = -0.273, -9.341/10.154 = -0.919, -2.857/10.154 = -0.281

```

---

<div class="post-metadata">

**Author:** ![CarloM](https://community.unix.com/letter_avatar/carlom/32/5_5575768a8748004e209b776fc1b2916d.png) [@CarloM](https://community.unix.com/u/CarloM)\
**Post date:** [April 4, 2012, 6:49am UTC](https://community.unix.com/t/vector-normalisation/307779/2 "2012-04-04T06:49:16Z")

</div>

```nohighlight
[/tmp] awk '{nm=sqrt(($1*$1)+($2*$2)+($3*$3)); print $1/nm FS $2/nm FS $3/nm}' file
-0.273 -0.919946 -0.281371
-0.600509 -0.770732 -0.212982
-0.429911 -0.884231 0.182515

```

---

<div class="post-metadata">

**Author:** ![chrisjorg](https://community.unix.com/letter_avatar/chrisjorg/32/5_5575768a8748004e209b776fc1b2916d.png) [@chrisjorg](https://community.unix.com/u/chrisjorg)\
**Post date:** [April 4, 2012, 6:55am UTC](https://community.unix.com/t/vector-normalisation/307779/3 "2012-04-04T06:55:03Z")

</div>

Why does the BEGIN/END commands in this script change the function of the script to loop over all records before doing the division by the norm?

```nohighlight
awk 'BEGIN{s=0}{s = s + $1^2 + $2^2 + $3^2}END{print sqrt(s)}' file`

```

---

<div class="post-metadata">

**Author:** ![balajesuri](https://community.unix.com/user_avatar/community.unix.com/balajesuri/32/1423_2.png) [@balajesuri](https://community.unix.com/u/balajesuri)\
**Post date:** [April 4, 2012, 7:03am UTC](https://community.unix.com/t/vector-normalisation/307779/4 "2012-04-04T07:03:48Z")

</div>

> [@chrisjorg](#):
>
> Why does the BEGIN/END commands in this script change the function of the script to loop over all records before doing the division by the norm?
> 
> ```plaintext
> awk 'BEGIN{s=0}{s = s + $1^2 + $2^2 + $3^2}END{print sqrt(s)}' file`
> 
> ```

Statements in BEGIN block will be executed before reading any line of file and statements in END block will be executed after all the lines in file are read.

So, initially 's' is set to 0. And `$1^2 + $2^2 + $3^2` of each line is added to 's' iteratively. After all the lines are read, sqrt(s) is printed.

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [April 4, 2012, 7:15am UTC](https://community.unix.com/t/vector-normalisation/307779/5 "2012-04-04T07:15:10Z")

</div>

General case:

```nohighlight
awk '{normp=0;for(i=1;i<=NF;i++)normp+=$i*$i; for(i=1;i<=NF;i++)printf "%8.3f",$i/sqrt(normp);print ""}' infile

```
