Calculating the maximum distance between elements of a vector in MATLAB

Suppose we have a vector like

x = -1:0.05:1;
ids = randperm(length(x));
x = x(ids(1:20));

I would like to calculate the maximum distance between elements in xsome idiomatic way. It would just be easy to sort through all possible combinations of elements x, but I feel that there may be a way to do this using the MATLAB built-in functions in some crazy but idiomatic way.

+3
source share
4 answers

What about

max_dist = max(x) - min(x)

?

+7
source

Do you mean the difference between the largest and smallest elements in your vector? If so, something like this will work:

max(x) - min(x)

, .

+2

, , . , , , . , , , , . , , x , :

xmin = min(x);
xmax = max(x);
maxdistance = max(x - xmin,xmax - x);

, (IPDM). , 1- . :

D = ipdm(x,'subset','farthest','result','struct');

, , , , .

+2
source

Uhh ... I would like to have MATLAB in my hands and still early in the morning, but what about something like:

max_dist = max(x(2:end) - x(1:end-1));

I do not know, this is what you are looking for.

+1
source

Source: https://habr.com/ru/post/1737101/


All Articles