Finding Middlemost row in a matrix

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

11 comentarios

Stephen23
Stephen23 el 6 de Nov. de 2017
What does "middlemost" mean ?
mr mo
mr mo el 6 de Nov. de 2017
In the matrix A, the first column values varied form 8.9505 to 9.2005, and the second column values varied form 4.8075 to 4.5575 . I want to find out which one of the rows of matrix A have the values in its first column and second column, that are the nearest values to the middle point between 8.9505 to 9.2005 and 4.8075 to 4.5575 respectively. Thanks.
Stephen23
Stephen23 el 6 de Nov. de 2017
What does "middle point" mean ?
mr mo
mr mo el 6 de Nov. de 2017
Imagine that the first column is X coordinates and second column is Y coordinates of some points. The middle point of column 1 is :
(8.9505 + 9.2005)/2 = 9.0755
and the middle point of column 2 is:
(4.8075 + 4.5575)/2 = 4.6825
I want to find which one of the row or rows of matrix A has the nearest values to these values in the first and second column respectively. Thanks
Birdman
Birdman el 6 de Nov. de 2017
Check my answer.
mr mo
mr mo el 6 de Nov. de 2017
I checked it, but it is not what I want. Thank you very much for your help.
Jan
Jan el 6 de Nov. de 2017
Editada: Jan el 6 de Nov. de 2017
@mr mo: You still do not explain, how "middlemost" is defined mathematically. You give one example, but it is not a clear and unique definition. It is not efficient to let the readers guess.
You mention "8.9505 + 9.2005" for the first column. This can be the smallest and largest value, or the first an the last- this is the same, because the 1st column seems to be sorted. But the second is not. Do you mean the first and last element or the smallest and largest?
You forgot to explain, what should happen, if multiple elements have the same value, if it is has minimal distance. What is "the middlemost point" in [1,2,3,4]? Or [1,2.5,2.5,4]?
What is the wanted output for your example? 19? Or A(19, :)? Or do you want an answer for each column?
You make it really hard to help you.
mr mo
mr mo el 6 de Nov. de 2017
Oh, you are right, excuse me for my fault, I mean the smallest and largest values, so the middle point of column 2 should be
(4.5575 + 4.8575)/2 = 4.7075
Jan
Jan el 6 de Nov. de 2017
Okay, this point is clear now. The proceed with the next step: How do you want to measure the distance: https://www.mathworks.com/matlabcentral/answers/365367-finding-middlemost-row-in-a-matrix#comment_501744
mr mo
mr mo el 6 de Nov. de 2017
At the end I want to find the row of matrix A that its first column and second column values is the nearest as possible as to the mid points that I was mentioned above. Thanks.
mr mo
mr mo el 6 de Nov. de 2017
Editada: mr mo el 6 de Nov. de 2017
for example this row may be the answer
A(19,:) = 9.0505 4.7325 1.6187 0.0020
because the 9.0505 is near to 9.0755 where is the midpoint of first column of A, and 4.7325 is near to 4.7075 where is the midpoint of second column of A.

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 6 de Nov. de 2017
Editada: Jan el 6 de Nov. de 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.

11 comentarios

mr mo
mr mo el 6 de Nov. de 2017
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 4.5575 to maximum 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
mr mo
mr mo el 6 de Nov. de 2017
Editada: mr mo el 6 de Nov. de 2017
for example this row may be the answer
A(19,:) = 9.0505 4.7325 1.6187 0.0020
because the 9.0505 is near to 9.0755 where is the midpoint of first column of A, and 4.7325 is near to 4.7075 where is the midpoint of second column of A.
mr mo
mr mo el 6 de Nov. de 2017
Sorry. This part of your code dose not work.
% (Squared) Euclidean distances of A(:, 1:2) to this point:
d = sum((A(:, 1:2) - meanValue).^2, 2)
Jan
Jan el 6 de Nov. de 2017
Editada: Jan el 6 de Nov. de 2017
@mr mo: It would be kind not only to claim, that the code "does not work", but to mention any details. It is inefficient to guess, what you want. It should be your interest to provide the information you have on your screen, instead of wasting the time of the readers, when they have to guess this.
This line does work on my Matlab R2016b system. Maybe you are using an older Matlab version without auto-expanding. This would be immediately clear, if you post the error message. Nevertheless, I guess again:
d = sum(bsxfun(@minus, A(:, 1:2), meanValue).^2, 2)
or
d = sum([A(:, 1) - meanValue(1), A(:, 2) - meanValue(2)].^2, 2)
Note that your sentence
"because the 9.0505 is near to 9.0755 where is the midpoint of first
column of A, and 4.7325 is near to 4.7075"
is still not meaningful. See the example I've given above:
A = [ 1, 1001;
50, 1002;
97, 1003;
98, 1050;
99, 1099];
The mean values are 50 and 1050. Choosing the 2nd row, because A(2,1) is near to 50 and choosing the 4th row, because A(4,2) is near to 1050 are both matching choices. But which one do you want? You repeatedly explain, that you want to use two different criteria to select one row. Tow times "near to" does not allow to select one index. I have asked your already, if you e.g. want to use the Euclidean norm between the vectors of A(:, 1:2) and the searched [1x2] vector.
And again: What should happen for: A = [1,1; 2,2; 3,3; 4,4]? The 2nd and the 3rd row have the same distance to the midpoint. What is the wanted answer then?
Please do not post the same comments multiple times. This does not add new information to the thread, but confuse the readers. It wastes time to read a comment only to see, that I have read it before already.
mr mo
mr mo el 6 de Nov. de 2017
Editada: mr mo el 6 de Nov. de 2017
you're right. this is the error.
d = sum((A(:, 1:2) - meanValue).^2, 2)
Error using -
Matrix dimensions must agree.
mr mo
mr mo el 6 de Nov. de 2017
I have Matlab R2015b. Is it necessary to update or not?
mr mo
mr mo el 6 de Nov. de 2017
In my example the difference between rows is not as much as your example
A = [ 1, 1001;
50, 1002;
97, 1003;
98, 1050;
99, 1099];
but I want to find the row with minimum distances of both mid points and these distances have same degree of importance. I think in this case, it is better to choose the row that have minimum distance. In your example the distances are :
98-50 = 48
1050 - 1002 = 48
So I think in this situation both of these rows will be accepted .
Jan
Jan el 6 de Nov. de 2017
Updating R2015b to at least R2016b would allow to use the smart auto-expanding. But you can use bsxfun as well.
I both rows should be replied, this is needed:
meanValue = (min(A(:, 1:2)) + max(A(:, 1:2))) / 2;
dist = sum(bsxfun(@minus, A(:, 1:2), meanValue).^2, 2)
minDist = min(dist);
isMin = (dist == minDist); % Logical indexing
midA = A(isMin, :)
If the indices are needed:
minIndex = find(isMin);
mr mo
mr mo el 6 de Nov. de 2017
The matrix A is a part of my whole data set that I want to do this process on it and the case
A = [1,1; 2,2; 3,3; 4,4]
does not exist in my data set as I know.
mr mo
mr mo el 6 de Nov. de 2017
Thank you very much for your help.
meanValue = (min(A(:, 1:2)) + max(A(:, 1:2))) / 2;
dist = sum(bsxfun(@minus, A(:, 1:2), meanValue).^2, 2)
minDist = min(dist);
isMin = (dist == minDist); % Logical indexing
midA = A(isMin, :)
Is your code calculate the Euclidean distances ? because I want to find the row with minimum Euclidean distances from mid points.
Jan
Jan el 9 de Nov. de 2017
The Euclidean distance is:
sqrt(sum(x.^2))
Then you need sqrt(minDist) to get the distance.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 6 de Nov. de 2017
Try mean() or median():
columnMedians = median(A, 1)
columnMeans = mean(A, 1)
depending on what "middlemost" means to you.

4 comentarios

mr mo
mr mo el 6 de Nov. de 2017
Editada: mr mo el 6 de Nov. de 2017
I want to find the row or rows in matrix A that have the middlemost values based on values in the first and second column of matrix A. I don't want the mean of columns, I want to find the row index of the row of matrix A that have the middlemost values of column 1 and middlemost values of column 2. Thanks.
Jan
Jan el 6 de Nov. de 2017
Editada: Jan el 6 de Nov. de 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.
mr mo
mr mo el 6 de Nov. de 2017
Editada: mr mo el 6 de Nov. de 2017
Imagine that the first column is X coordinates and second column is Y coordinates of some points. 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 row or rows of matrix A has the nearest values to these values in the first and second column respectively. Thanks

Iniciar sesión para comentar.

Preguntada:

el 6 de Nov. de 2017

Comentada:

Jan
el 9 de Nov. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by