Finding the maximum output value and corrosponding input for a function
2 次查看(过去 30 天)
显示 更早的评论
I have a function that essentially calculates the distance beetween a set of points p1,p2,p3 and q1,q2,q3. I am looping through multiple sets of p1,p2,p3 and comparing against the q values which stay constant. I want to find which set of inputs of P maximises the distance beetween p and q. Does anyone know a way to do this?
For examples;
data(: , :, 1) = [1 2 3]
data(: , :, 2) = [4 5 6]
data(: , :, 3) = [7 8 9]
my input is data, a 1x3x3 array but the general input will always be a 1xnx3 array.
Q will be the calculated median values for each channel/layer of data.
Q = [2 5 8]
Now I loop through each column of data, finding max distance.
for this case max distance is resulted from data(1,1,:) and data(1,3,:)
i would want it to always choose the first set of data that produces max.
How can i get a code on matlab to do this? any tips/tricks/help would be much appreciated
0 个评论
回答(1 个)
Bob Thompson
2019-9-9
I'm going to make some assumptions with this answer, but I will try to explain them.
The first assumption is that you know how to index a range of values in a matrix.
The second assumption is that you are looking for the distance between all points in your set, to find the maximum distance between any two points, rather that only comparing against the first point. If this is not a correct assumption you can still use the method below, just restrict the answer range in the max command.
I am also assuming you have access to the pdist command. This is technically in the statistics toolbox.
distances = squareform(pdist(data));
[r,c] = find(distances==max(distances(:)));
(r,c) should contain two values each, which make up an ordered pair and its reverse for the location of the maximum distance (because squareform). The two points which create the distance should be the values in r, the values in c, or the first of each, or the last of each. It doesn't really matter, because all of those pairs are the same.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!