11 views (last 30 days)

Hi. Suppose I have a (m*n) matrix A e.g.

A=[8.9505 4.8075 1.6187 0.0020

8.9755 4.7575 1.6187 0.0020

8.9755 4.7825 1.6187 0.0020

8.9755 4.8075 1.6187 0.0020

8.9755 4.8325 1.6187 0.0020

8.9755 4.8575 1.6187 0.0020

9.0005 4.7325 1.6187 0.0020

9.0005 4.7575 1.6187 0.0020

9.0005 4.7825 1.6187 0.0020

9.0005 4.8075 1.6187 0.0020

9.0005 4.8325 1.6187 0.0020

9.0005 4.8575 1.6187 0.0020

9.0255 4.7325 1.6187 0.0020

9.0255 4.7575 1.6187 0.0020

9.0255 4.7825 1.6187 0.0020

9.0255 4.8075 1.6187 0.0020

9.0255 4.8325 1.6187 0.0020

9.0255 4.8575 1.6187 0.0020

9.0505 4.7325 1.6187 0.0020

9.0505 4.7575 1.6187 0.0020

9.0505 4.7825 1.6187 0.0020

9.0505 4.8075 1.6187 0.0020

9.0505 4.8325 1.6187 0.0020

9.0755 4.7575 1.6187 0.0020

9.1255 4.6075 1.6187 0.0020

9.1505 4.5825 1.6187 0.0020

9.1505 4.6075 1.6187 0.0020

9.1755 4.5825 1.6187 0.0020

9.2005 4.5575 1.6187 0.0020];

Imagine that the first column is X coordinates and second column is Y coordinates of some points.

In the matrix A, the first column values varied form minimum value 8.9505 to maximum value 9.2005, and the second column values varied form minimum value 4.5575 to maximum value 4.8575

The middle point of column 1 is :

(8.9505 + 9.2005)/2 = 9.0755

and the middle point of column 2 is:

(4.5575 + 4.8575)/2 = 4.7075

I want to find which one of the rows of matrix A has the nearest values to these values in its first and second columns respectively. Thanks

Jan
on 6 Nov 2017

Edited: Jan
on 6 Nov 2017

Perhaps - a bold guess:

meanValue = (min(A(:, 1:2)) + max(A(:, 1:2))) / 2;

dist = sum((A(:, 1:2) - meanValue).^2, 2); % >= 2016b: Auto-Expand

% With older Matlab versions:

% dist = sum(bsxfun(@minus, A(:, 1:2), meanValue).^2, 2)

[minDist, minIndex] = min(dist)

midA = A(minIndex, :)

For your example:

% The center between the minimal and maximal points:

meanValue = [9.0755, 4.7075]

% (Squared) Euclidean distances of A(:, 1:2) to this point:

d = sum((A(:, 1:2) - meanValue).^2, 2)

% Minimal distance:

[minDist, minIndex] = min(d);

minIndex = 19

% Or do you want multiple outputs, if the minimal distance occurs

% multiple times?

??? minDist = min(d);

??? minIndex = find(d == minDist);

But perhaps you look for something else. Unfortunately you do not provide a mathematical definition of what you want, although you have been asked repeatedly.

Jan
on 9 Nov 2017

The Euclidean distance is:

sqrt(sum(x.^2))

Then you need sqrt(minDist) to get the distance.

Image Analyst
on 6 Nov 2017

Try mean() or median():

columnMedians = median(A, 1)

columnMeans = mean(A, 1)

depending on what "middlemost" means to you.

Jan
on 6 Nov 2017

@mr mo: You still did not define, what "middlemost" means. Now we know, that it is "based on values in the first and second column of matrix A", but how is it determined without using a kind of mean?

You have used "middlemost" 18 times now, but it is not clear, what its mathematical meaning is.

Opportunities for recent engineering grads.

Apply Today
## 11 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501648

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501648

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501706

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501706

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501711

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501711

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501715

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501715

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501719

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501719

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501721

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501721

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501727

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501727

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501739

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501739

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501746

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501746

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501760

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501760

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501764

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501764

Sign in to comment.