-3

Array question

I don't understand this question. Actually just this part; "Given two vectors of length n that are represented with one-dimensional arrays"

I use two vectors but I don't know what value they have.

For example, vector can be a = [1,2,3] but I don't know exactly what are they? What do they have? Maybe it is a = [3,4,5].

martineau
  • 119,623
  • 25
  • 170
  • 301
bkk
  • 81
  • 1
  • 7
  • https://en.wikipedia.org/wiki/Euclidean_distance – Thiyagu Mar 31 '18 at 16:06
  • The vectors are `a` and `b`; it doesn't matter what the values are, that's why you have the index `a[i]` and `b[i]` for abstract notation. – dROOOze Mar 31 '18 at 16:08
  • I know euclidean distance. just wondering what value they are. for example; n = 3 a = [1,2,3] b = [4,5,6] Can I solve this problem with these? – bkk Mar 31 '18 at 16:12
  • the point is to write code that will work for _any_ values – avigil Mar 31 '18 at 16:25
  • Larry Page has not personally looked at every page on the internet, but PageRank works anyway, and google search shows you some results. Guido van Rossum has not read your Python program, but the Python interpreter works with your code anyway. Why do you think that you have to know anything about the values of `a` and `b` in order to be able to compute with them? That's the whole point of abstracting and automating: you have to express the algorithm in such a form that it can work without any supernatural interventions by divine human beings. If cannot know every input to every program – Andrey Tyukin Mar 31 '18 at 16:31
  • since you are only being asked to write a code fragment, picking two arbitrary vectors would be fine but understand that the code should not depend on having specific values – avigil Mar 31 '18 at 16:31
  • I did it now. of course I can't know everything you are right.and thank you all of you. – bkk Mar 31 '18 at 16:36
  • 1
    @Andrey Tyukin: Suggesting `pandas` or using it as an answer is total overkill, especially to someone at this point in learning Python — so, no, this is **not** a duplicate of that question in my opinion. – martineau Mar 31 '18 at 17:51
  • @martineau As my later comment hopefully indicates, I also came to the conclusion that the OP doesn't seem to understand something much more basic (variables?). So, I agree in general, it's not really a duplicate of what I initially linked. However, I didn't suggest `pandas` anywhere, you probably meant `numpy`. – Andrey Tyukin Mar 31 '18 at 17:55
  • @AndreyTyukin: Yes, I meant `numpy`. Regardless, if that's the case, perhaps you should delete your earlier comment about this being a duplicate of a question about using it. – martineau Mar 31 '18 at 18:01
  • 1
    @martineau Alright, retracted close vote. I will leave [the link to the question about euclidean distance](https://stackoverflow.com/questions/1401712/how-can-the-euclidean-distance-be-calculated-with-numpy) anyway, because maybe not everyone who wants to compute L2-distance and comes across this question wants to reimplement it from scratch. – Andrey Tyukin Mar 31 '18 at 18:04

3 Answers3

2

You don't need numpy do something as simple as this.
Instead just translate the formula into Python code:

import math

a = [1, 2, 3]
b = [3, 4, 5]
n = len(a)

# Compute Euclidean distance between vectors "a" and "b".

# First sum the squares of the difference of each component of vectors.
distance = 0
for i in range(n):
    difference = a[i] - b[i]
    distance += difference * difference

# The answer is square root of those summed differences.
distance = math.sqrt(distance)
print(distance)  # -> 3.4641016151377544
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Actually, I have already did it. but you did very understandable, it is a little bit different than from mine and more good than mine of course.. If anyone see this code in anytime. it is so good thank you. – bkk Mar 31 '18 at 19:01
0

Your task is to write code that computes the value if the vectors a and b are given. Your job is not to write down a number.

You could start with this:

distance = 0
for value in a:
    [your code]

print(distance)
Niklas Mertsch
  • 1,399
  • 12
  • 24
  • Then write it without a function. – Matthias Mar 31 '18 at 16:24
  • just use the code underneath the 'def' line. add a=[1,2,3], and b=[4,5,6] at the top. Functions are how you would do this for any given vectors but you can write code by just picking two arbitrary examples – avigil Mar 31 '18 at 16:26
  • 1
    I understand now. i did it. it works. thank you very much all of you. – bkk Mar 31 '18 at 16:35
-1

You could use numpy. Your so called vectors would then correspond to numpy arrays.

import numpy as np
np.sqrt(np.sum(np.power(a-b,2)))

You might need to add this before

a, b = np.array(a),np.array(b)
zar3bski
  • 2,773
  • 7
  • 25
  • 58