From: Diego Lass on
Hi there,
I want to classify data points based on its distance from two values, example

x = [1 2 3 4 5];
y = 2.3;
z = 4.5;

I want all the points less than y classified as 0, greater than z classified as 1. And if between y and z, classify according to its distance to y and z (take the shorter distance)
Result for above is
result = [0 0 0 1 1]

How can I do this as efficient as possible? Thanks
Diego


From: Walter Roberson on
Diego Lass wrote:

> I want to classify data points based on its distance from two values,
> example
>
> x = [1 2 3 4 5];
> y = 2.3;
> z = 4.5;
>
> I want all the points less than y classified as 0, greater than z
> classified as 1. And if between y and z, classify according to its
> distance to y and z (take the shorter distance)
> Result for above is
> result = [0 0 0 1 1]
>
> How can I do this as efficient as possible?

What if something is exactly half way between the y and z?

The below will count the exact half way point as rounding to z.

[counts, result] = histc(x, [(y+z)/2 inf]);

Unless, that is, one of the x can actually be inf, as that would return
a bin of 2.
From: ImageAnalyst on
Diego:
A little verbose and explicit perhaps but that's just to help you
understand it better. But it works, and without any loops.

% Sample data:
x = [1 2 3 4 5];
y = 2.3;
z = 4.5;

% Prepare an output matrix.
output = x;
% Assign the easy ones.
output(x < y) = 0;
output(x > z) = 1;
indexesAlreadyAssigned = (x < y) | (x > z)
% Find out the distance from x to y.
differenceFromY = abs(x-y)
% Find out the distance from x to z.
differenceFromZ = abs(x-z)
% Find out where distance to y is less than distance to z.
takeYif = differenceFromY < differenceFromZ
% Find out where distance to z is less than distance to y.
takeZif = differenceFromZ < differenceFromY
% Zero out indexes that we've already used.
takeYif(indexesAlreadyAssigned) = 0
takeZif(indexesAlreadyAssigned) = 0
% For the "in between" values, take the one that's closest.
output(takeYif) = 0;
output(takeZif) = 1;
% Display the output.
output

indexesAlreadyAssigned =
1 1 0 0 1

differenceFromY =
1.3000 0.3000 0.7000 1.7000 2.7000

differenceFromZ =
3.5000 2.5000 1.5000 0.5000 0.5000

takeYif =
1 1 1 0 0

takeZif =
0 0 0 1 1

takeYif = 0 0 1 0 0

takeZif =
0 0 0 1 0

output =
0 0 0 1 1


From: Diego Lass on
Thanks!
Diego

ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <92f32a40-4a1c-4cf5-9a16-ec5a574fdb39(a)c22g2000vbb.googlegroups.com>...
> Diego:
> A little verbose and explicit perhaps but that's just to help you
> understand it better. But it works, and without any loops.
>
> % Sample data:
> x = [1 2 3 4 5];
> y = 2.3;
> z = 4.5;
>
> % Prepare an output matrix.
> output = x;
> % Assign the easy ones.
> output(x < y) = 0;
> output(x > z) = 1;
> indexesAlreadyAssigned = (x < y) | (x > z)
> % Find out the distance from x to y.
> differenceFromY = abs(x-y)
> % Find out the distance from x to z.
> differenceFromZ = abs(x-z)
> % Find out where distance to y is less than distance to z.
> takeYif = differenceFromY < differenceFromZ
> % Find out where distance to z is less than distance to y.
> takeZif = differenceFromZ < differenceFromY
> % Zero out indexes that we've already used.
> takeYif(indexesAlreadyAssigned) = 0
> takeZif(indexesAlreadyAssigned) = 0
> % For the "in between" values, take the one that's closest.
> output(takeYif) = 0;
> output(takeZif) = 1;
> % Display the output.
> output
>
> indexesAlreadyAssigned =
> 1 1 0 0 1
>
> differenceFromY =
> 1.3000 0.3000 0.7000 1.7000 2.7000
>
> differenceFromZ =
> 3.5000 2.5000 1.5000 0.5000 0.5000
>
> takeYif =
> 1 1 1 0 0
>
> takeZif =
> 0 0 0 1 1
>
> takeYif = 0 0 1 0 0
>
> takeZif =
> 0 0 0 1 0
>
> output =
> 0 0 0 1 1
>
From: Greg Heath on
On May 24, 7:15 pm, "Diego Lass" <dlISC...(a)gmail.com> wrote:
> Hi there,
> I want to classify data points based on its distance from two values, example
>
> x = [1 2 3 4 5];
> y = 2.3;
> z = 4.5;
>
> I want all the points less than y classified as 0, greater than z classified as 1.  And if between y and z, classify according to its distance to y and z (take the shorter distance)
> Result for above is
> result = [0 0 0 1 1]
>
> How can I do this as efficient as possible?  Thanks
> Diego

compare abs(x-y) with abs(x-z)

Hope this helps.

Greg