Reputation: 9816
I have column which is used to store a physical vector quantity, say force or speed, in the following format:
[0 0]
[-0.011661058152260357 0.00372367132268286]
I wish to insert a new column which the magnitude of this vector. Calculation is simple: sqrt(x^2 + y^2)
, where x
is the first number and y
the second.
The problem is how to read them and compute.
How can I achieved this in Excel
or R
or Matlab
any way?
Upvotes: 0
Views: 2570
Reputation: 59485
In Excel:
=SQRT(MID(A1,2,FIND(" ",A1)-1)^2+(MID(A1,FIND(" ",A1),FIND("]",A1)-FIND(" ",A1)-1)^2))
Upvotes: 2
Reputation: 6223
If these numbers were stored in a data frame in R
, then you could simply put the magnitude in a new column:
df <- data.frame(x = rnorm(5), y = rnorm(5)) # fake data
df$mag <- sqrt(df$x^2 + df$y^2)
which gives:
x y mag
1 -0.1027138 0.8126019 0.8190678
2 2.4021159 -0.1336111 2.4058289
3 -1.7490194 0.8151816 1.9296605
4 0.4898490 1.2700167 1.3612106
5 -1.6132449 2.2485643 2.7674176
Upvotes: 2