Re: Calculating Minkowski distance between two rows

From: Babak Alipour <babak(dot)alipour(at)gmail(dot)com>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Calculating Minkowski distance between two rows
Date: 2016-04-25 14:26:07
Message-ID: CA+_of16Y3TGukHzWmc+T2qMiX7-i6aOfikLW0yUjt9i4C+gynA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

That is correct. The function I've written only works when the two tables
are named table_train and table_test; is it possible to generalize that to
take in any two tables?

Thanks in advance.

>Babak

On Mon, Apr 25, 2016 at 10:24 AM, Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
wrote:

> On 04/25/2016 07:07 AM, Babak Alipour wrote:
>
>> Greetings everyone,
>>
>> I'm a novice plpgsql user.
>> For an application, I'm trying to write a user-defined function that
>> takes a row of some table (let's say with k fields) and takes another
>> row from another table (again with k fields); then calculate the
>> Euclidean, Manhattan or generally Minkowski distance (with some p) and
>> then return an integer.
>> I've written this:
>>
>> CREATE FUNCTION euclidean_distance(row1 table_train, row2 table_test,
>> OUT distance DOUBLE PRECISION) AS $$
>> DECLARE
>> tmp DOUBLE PRECISION;
>> BEGIN
>> FOR col IN SELECT column_name FROM information_schema.columns WHERE
>> table_name=table_train LOOP
>> tmp := (row1.col - row2.col);
>> distance += tmp*tmp;
>> END LOOP;
>> distance := sqrt(distance);
>> END;
>> $$ LANGUAGE plpgsql;
>>
>> Could anyone please help me fix this function so that I can pass any two
>> rows of two tables (with same number of columns) and have their distance
>> returned.
>>
>
> You are already doing that, so do you mean any two rows of any two tables?
>
>
>> Best regards,
>> Babak Alipour
>>
>> --
>> */Babak Alipour ,/*
>> */University of Florida/*
>>
>
>
> --
> Adrian Klaver
> adrian(dot)klaver(at)aklaver(dot)com
>

--
*Babak Alipour ,*
*University of Florida*

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bráulio Bhavamitra 2016-04-25 14:29:35 Re: Columnar store as default for PostgreSQL 10?
Previous Message Adrian Klaver 2016-04-25 14:24:43 Re: Calculating Minkowski distance between two rows