How to find the input value when outputting the maximum value

조회 수: 2 (최근 30일)
文辉 沈
文辉 沈 2022년 4월 30일
댓글: 文辉 沈 2022년 4월 30일
[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?

채택된 답변

Walter Roberson
Walter Roberson 2022년 4월 30일
편집: Walter Roberson 2022년 4월 30일
[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
文辉 沈
文辉 沈 2022년 4월 30일
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
文辉 沈
文辉 沈 2022년 4월 30일
I still thank you very much, your help is very useful to me

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Design Condition Indicators Interactively에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by