How to find the input value when outputting the maximum value

5 vues (au cours des 30 derniers jours)
文辉 沈
文辉 沈 le 30 Avr 2022
Commenté : 文辉 沈 le 30 Avr 2022
[mi] = [dab,dac,dad,dbc,dbd,dcd];
length = max(mi);
The coordinates of the four points A, B, C, and D are known. I have calculated the distance between the two points through pdist2(). I used max() to get the maximum value of these distances. My current requirement is to convert the maximum The two points are connected by a plot() line, so how can I know the coordinates of the two points of the maximum value?

Réponse acceptée

Walter Roberson
Walter Roberson le 30 Avr 2022
Modifié(e) : Walter Roberson le 30 Avr 2022
[max_length, maxidx] = max(mi);
switch maxidx
case 1
coords = {A, B};
case 2
coords = {A, C};
case 3
coords = {A, D};
case 4
coords = {B, C};
case 5
coords = {B, D};
case 6
coords = {C, D};
otherwise
error('strange maximum index!')
end
... It would have been easier to work with the array returned by pdist2() directly.
all_coords = {A, B, C, D};
[max_length, maxidx] = max( reshape(triu(Distance_Matrix), [], 1 );
[R,C] = ind2sub( size(Distance_Matrix), maxidx );
coords = {all_coords{R}, all_coords{C}};
  2 commentaires
文辉 沈
文辉 沈 le 30 Avr 2022
Actually, my problem is a bit complicated, but I've solved it for the time being (it's repetitive work though)
Because the abscissa and ordinate of the four points of abc are changing, we have to use such a cumbersome method. Maybe, do you have a script that can be simplified?
if rank(find(bw(1,:))) ~= 0 && rank(find(bw(:,1))) ~= 0
xa = find(bw(1,:), 1, 'first');
xb = find(bw(1,:), 1, 'last');
xc = find(bw(:,1), 1, 'first');
xd = find(bw(:,1), 1, 'last');
dab = pdist2([xa, 1],[xb, 1]);
dac = pdist2([xa, 1],[1, xc]);
dad = pdist2([xa, 1],[1, xd]);
dbc = pdist2([xb, 1],[1, xc]);
dbd = pdist2([xb, 1],[1, xd]);
dcd = pdist2([1, xc],[1, xd]);
[mi] = [dab,dac,dad,dbc,dbd,dcd];
[length, maxidx] = max(mi);
switch maxidx
case 1
coord1 = [xa, 1];
coord2 = [xb, 1];
case 2
coord1 = [xa, 1];
coord2 = [1, xc];
case 3
coord1 = [xa, 1];
coord2 = [1, xd];
case 4
coord1 = [xb, 1];
coord2 = [1, xc];
case 5
coord1 = [xb, 1];
coord2 = [1, xd];
case 6
coord1 = [1, xc];
coord2 = [1, xd];
otherwise
error('strange maximum index!')
end
elseif rank(find(bw(1,:))) ~= 0 && rank(find(bw(end,:))) ~= 0
xa = find(bw(1,:), 1, 'first');
xb = find(bw(1,:), 1, 'last');
xc = find(bw(end,:), 1, 'first');
xd = find(bw(end,:), 1, 'last');
dab = pdist2([xa, 1],[xb, 1]);
dac = pdist2([xa, 1],[xc, r]);
dad = pdist2([xa, 1],[xd, r]);
dbc = pdist2([xb, 1],[xc, r]);
dbd = pdist2([xb, 1],[xd, r]);
dcd = pdist2([xc, r],[xd, r]);
[mi] = [dab,dac,dad,dbc,dbd,dcd];
[length, maxidx] = max(mi);
switch maxidx
case 1
coord1 = [xa, 1];
coord2 = [xb, 1];
case 2
coord1 = [xa, 1];
coord2 = [xc, r];
case 3
coord1 = [xa, 1];
coord2 = [xd, r];
case 4
coord1 = [xb, 1];
coord2 = [xc, r];
case 5
coord1 = [xb, 1];
coord2 = [xd, r];
case 6
coord1 = [xc, r];
coord2 = [xd, r];
otherwise
error('strange maximum index!')
end
elseif rank(find(bw(1,:))) ~= 0 && rank(find(bw(:,end))) ~= 0
xa = find(bw(1,:), 1, 'first');
xb = find(bw(1,:), 1, 'last');
xc = find(bw(:,end), 1, 'first');
xd = find(bw(:,end), 1, 'last');
dab = pdist2([xa, 1],[xb, 1]);
dac = pdist2([xa, 1],[c, xc]);
dad = pdist2([xa, 1],[c, xd]);
dbc = pdist2([xb, 1],[c, xc]);
dbd = pdist2([xb, 1],[c, xd]);
dcd = pdist2([c, xc],[c, xd]);
[mi] = [dab,dac,dad,dbc,dbd,dcd];
[length, maxidx] = max(mi);
switch maxidx
case 1
coord1 = [xa, 1];
coord2 = [xb, 1];
case 2
coord1 = [xa, 1];
coord2 = [c, xc];
case 3
coord1 = [xa, 1];
coord2 = [c, xd];
case 4
coord1 = [xb, 1];
coord2 = [c, xc];
case 5
coord1 = [xb, 1];
coord2 = [c, xd];
case 6
coord1 = [c, xc];
coord2 = [c, xd];
otherwise
error('strange maximum index!')
end
elseif rank(find(bw(:,1))) ~= 0 && rank(find(bw(end,:))) ~= 0
xa = find(bw(:,1), 1, 'first');
xb = find(bw(:,1), 1, 'last');
xc = find(bw(end,:), 1, 'first');
xd = find(bw(end,:), 1, 'last');
dab = pdist2([1, xa],[1, xb]);
dac = pdist2([1, xa],[xc, r]);
dad = pdist2([1, xa],[xd, r]);
dbc = pdist2([1, xb],[xc, r]);
dbd = pdist2([1, xb],[xd, r]);
dcd = pdist2([xc, r],[xd, r]);
[mi] = [dab,dac,dad,dbc,dbd,dcd];
[length, maxidx] = max(mi);
switch maxidx
case 1
coord1 = [1, xa];
coord2 = [1, xb];
case 2
coord1 = [1, xa];
coord2 = [xc, r];
case 3
coord1 = [1, xa];
coord2 = [xd, r];
case 4
coord1 = [1, xb];
coord2 = [xc, r];
case 5
coord1 = [1, xb];
coord2 = [xd, r];
case 6
coord1 = [xc, r];
coord2 = [xd, r];
otherwise
error('strange maximum index!')
end
elseif rank(find(bw(:,1))) ~= 0 && rank(find(bw(:,end))) ~= 0
xa = find(bw(:,1), 1, 'first');
xb = find(bw(:,1), 1, 'last');
xc = find(bw(:,end), 1, 'first');
xd = find(bw(:,end), 1, 'last');
dab = pdist2([1, xa],[1, xb]);
dac = pdist2([1, xa],[c, xc]);
dad = pdist2([1, xa],[c, xd]);
dbc = pdist2([1, xb],[c, xc]);
dbd = pdist2([1, xb],[c, xd]);
dcd = pdist2([c, xc],[c, xd]);
[mi] = [dab,dac,dad,dbc,dbd,dcd];
[length, maxidx] = max(mi);
switch maxidx
case 1
coord1 = [1, xa];
coord2 = [1, xb];
case 2
coord1 = [1, xa];
coord2 = [c, xc];
case 3
coord1 = [1, xa];
coord2 = [c, xd];
case 4
coord1 = [1, xb];
coord2 = [c, xc];
case 5
coord1 = [1, xb];
coord2 = [c, xd];
case 6
coord1 = [c, xc];
coord2 = [c, xd];
otherwise
error('strange maximum index!')
end
elseif rank(find(bw(end,:))) ~= 0 && rank(find(bw(:,end))) ~= 0
xa = find(bw(end,:), 1, 'first');
xb = find(bw(end,:), 1, 'last');
xc = find(bw(:,end), 1, 'first');
xd = find(bw(:,end), 1, 'last');
dab = pdist2([xa, r],[xb, r]);
dac = pdist2([xa, r],[c, xc]);
dad = pdist2([xa, r],[c, xd]);
dbc = pdist2([xb, r],[c, xc]);
dbd = pdist2([xb, r],[c, xd]);
dcd = pdist2([c, xc],[c, xd]);
[mi] = [dab,dac,dad,dbc,dbd,dcd];
[length, maxidx] = max(mi);
switch maxidx
case 1
coord1 = [xa, r];
coord2 = [xb, r];
case 2
coord1 = [xa, r];
coord2 = [c, xc];
case 3
coord1 = [xa, r];
coord2 = [c, xd];
case 4
coord1 = [xb, r];
coord2 = [c, xc];
case 5
coord1 = [xb, r];
coord2 = [c, xd];
case 6
coord1 = [c, xc];
coord2 = [c, xd];
otherwise
error('strange maximum index!')
end
end
文辉 沈
文辉 沈 le 30 Avr 2022
I still thank you very much, your help is very useful to me

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Predictive Maintenance Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by