How to find the minimum difference between the 3 elements of a vector in app designer?

I need to group the elements of a matrix 3 by 3 with the minimum difference between them. I found something like that: min(min(abs(X(1)-X2))), but l have 3 values.

6 comentarios

Let's say the input is this -
y = magic(3)
y = 3×3
8 1 6 3 5 7 4 9 2
What should be the output? and what logic is followed to obtain the output?
I have a matrix like:
X = rand(5,1)
X = 5×1
0.6076 0.2072 0.0161 0.2327 0.9077
The output should be the three of them with minumum difference among them, [0.0161 0.2327 0.2072]
What is the minimum difference here? How is it calculated?
This is actually what l'm asking for. Output is three of the elements of X with minimum difference among them.
I am asking what logic did you follow to get that output from that particular input.
How do you define minimum difference?
It was an axample. l didn't use anything, just looked at the numbers and wrote it.

Iniciar sesión para comentar.

 Respuesta aceptada

I take this to mean the three elements of X with the smallest extreme spread. I don't see that pdist2() would work for that.
This might work, so long as the number of elements in X stays small.
X = randi([0 99],3,3)
X = 3×3
65 46 98 71 61 28 91 4 26
C = nchoosek(X(:),3);
Crange = range(C,2);
row = Crange == min(Crange);
C(row,:)
ans = 1×3
65 71 61
From what's given, there's no reason to think that there's a unique minimum combination. The first example @Dyuman Joshi gave is a good demonstration. The example I gave will return all combinations with the same minimum spread.

7 comentarios

Thanks @DGM, it worked exactly. Can we make it to give all the elements in the matrix grouped in threes?
That's what C is. C is the set of possible combinations of the elements of X, if taken 3 at a time. Obviously, the size of C will increase rapidly as the number of elements of X increases.
l mean if we have 12 elements in the matrix X, can we make the code to give 4 groups from X which provide the requirement?
You mean combinations of length 4?
X = randi([0 99],3,4) % 12 elements
X = 3×4
33 42 21 23 34 45 21 38 66 15 86 10
C = nchoosek(X(:),4); % all combinations of length 4
Crange = range(C,2);
row = Crange == min(Crange);
C(row,:)
ans = 1×4
15 21 21 23
No, l meant grouping all elments 3 by 3, so length is the same as 3. But it gives other combinations giving minimum distance between them. In the code above, it gives an only C array, but there rest of the matrix X stays there untouched. I wanted to group all elements till there is none of them left.
Actually, tried and made it by deleting the elements in X linked with C array, then l'm writing this combinations to a table.
It would be grate if you have any other idea.
Thank you for your responses @DGM!
I still don't understand. How does a 3x3 array have 12 elements, and where does 4 come in?
Are you trying to find the minimal combinations left in X after removing each minimal combination?
-Are you trying to find the minimal combinations left in X after removing each minimal combination?
+Absolutely!

Iniciar sesión para comentar.

Categorías

Más información sobre Develop Apps Using App Designer en Centro de ayuda y File Exchange.

Preguntada:

el 11 de Dic. de 2023

Comentada:

el 13 de Dic. de 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by