I would like to find out if each line with its six values in matrix U has closer values to the line of the six values in matrix MA or to the line of values in matrix TA? (by the way these are time values)so if I find f.ex. line one in U is more like the line in MA, so it means that the happening is the same as happening in MA.
MA =
0 4.4710 8.8828 13.6977 17.7027 21.7184
TA =
0 5.0490 9.3733 13.6038 17.2513 20.5759
and
U =
0 3.4874 8.3723 12.8400 15.7461 21.0089
0 3.6776 8.4321 12.9606 15.7762 21.0557
0 5.5318 8.2832 12.8565 17.5710 20.9515
0 5.6120 8.4264 12.8430 17.5558 20.9401
0 5.6243 10.0483 12.8954 17.7006 22.3821
0 5.5102 10.0276 12.8021 17.5814 22.2412
0 3.1558 7.6719 12.2234 15.1850 19.9813
0 3.1794 7.6655 12.3352 15.4489 20.0210
0 4.7771 7.5656 12.2135 17.4471 19.8625
0 4.7598 7.5804 12.2065 17.5210 20.0173
Is it possible? how can one do this?
*PS: I have heard that knnsearch might could help, but have no idea how it does. maybe someone could show?
https://se.mathworks.com/help/stats/exhaustivesearcher.knnsearch.html

 채택된 답변

Star Strider
Star Strider 2021년 11월 11일

0 개 추천

One option is pdist2
MA = [0 4.4710 8.8828 13.6977 17.7027 21.7184];
TA = [0 5.0490 9.3733 13.6038 17.2513 20.5759];
U = [0 3.4874 8.3723 12.8400 15.7461 21.0089
0 3.6776 8.4321 12.9606 15.7762 21.0557
0 5.5318 8.2832 12.8565 17.5710 20.9515
0 5.6120 8.4264 12.8430 17.5558 20.9401
0 5.6243 10.0483 12.8954 17.7006 22.3821
0 5.5102 10.0276 12.8021 17.5814 22.2412
0 3.1558 7.6719 12.2234 15.1850 19.9813
0 3.1794 7.6655 12.3352 15.4489 20.0210
0 4.7771 7.5656 12.2135 17.4471 19.8625
0 4.7598 7.5804 12.2065 17.5210 20.0173];
[DMA,IMA] = pdist2(U,MA, 'euclidean', 'Smallest',1)
DMA = 1.6727
IMA = 3
[DTA,ITA] = pdist2(U,TA, 'euclidean', 'Smallest',1)
DTA = 1.4205
ITA = 4
So here the thiird row of ‘U’ is closest to ‘MA’ and the fourth row closeest to ‘TA’.
.

댓글 수: 12

Nicle Davidson
Nicle Davidson 2021년 11월 11일
I see, can I try all the rows in U and see about each row like row 1 is it closest to MA or is it closest to TA? ok for example MA, ok ,then row 2...
all rows, finding about each, which row are they closest to, MA or TA from row 1 to row 10, does not need even be automated, like a for loop or those is not a must, but tryin each of U rows is important, to see for each row is this row closest to TA or to MA, for example if I could use U(1) for first row and then try second row,
is it possible?
Nicle Davidson
Nicle Davidson 2021년 11월 11일
to explain what it gives, each row in U is timestamps for a happening. the timestamps pr row in this try are compared with the timestamps in MA and in TA, så one can see the happening is an MA happening or is it a TA happening. if it helps to understand it better?
Nicle Davidson
Nicle Davidson 2021년 11월 11일
편집: Nicle Davidson 2021년 11월 11일
But maybe I could get this out of this line manually?
[DMA,IMA] = pdist2(U,MA, 'euclidean', 'Smallest',1)
Can you please explain what this line of your code do to me
May be I could manually compare each line of U with both MA and TA? and line by line so I know each line is closer to TA or to MA?
I would accept even if I could do it manually line by line of U with both MA and TA, so then we are there.
Star Strider
Star Strider 2021년 11월 11일
The code returns the row of ‘U’ that is closest to the vector to be matched, ‘TA’ or ‘MA’ respectively in each line.
The line of code quoted in this Comment returns the closest matching row in ‘U’ to ‘MA’.
.
Nicle Davidson
Nicle Davidson 2021년 11월 12일
I mean can you explain the the line of your code part by part what they do please?
Star Strider
Star Strider 2021년 11월 12일
My code simply calls the pdist2 function. The function itself loops though each row of ‘U’, subtracts the chosen vector from it, calculates the Euclidean norm of the result (the square root of the sum of the squared elements) to calculate the distance, and since I asked it to report the least distance and the index of that vector, sorts them from smallest to largest, and reports the one I asked for.
That’s essentially the entire code. I could have written it as a for loop, however since pdist2 does all that already, there is no need to.
.
Nicle Davidson
Nicle Davidson 2021년 11월 12일
Well the task need to be as:
not which line in U is closes to MA or TA
but:
each line it self is it closer to MA or TA, and need to have one answer per line of the U matrix if you understand what I mean.
so not one answer for all the lines, but one answer per line. you know
Nicle Davidson
Nicle Davidson 2021년 11월 12일
Maybe I could run your code line by line for U? or something like that, like reducing U to each line each time manually and see what is the answer? or something alike?
Nicle Davidson
Nicle Davidson 2021년 11월 12일
However looks like I can not run this command. I get this error:
'pdist2' requires Statistics and Machine Learning Toolbox.
Yes, it does.
That said, this seems to be a moving target.
Try this —
MA = [0 4.4710 8.8828 13.6977 17.7027 21.7184];
TA = [0 5.0490 9.3733 13.6038 17.2513 20.5759];
U = [0 3.4874 8.3723 12.8400 15.7461 21.0089
0 3.6776 8.4321 12.9606 15.7762 21.0557
0 5.5318 8.2832 12.8565 17.5710 20.9515
0 5.6120 8.4264 12.8430 17.5558 20.9401
0 5.6243 10.0483 12.8954 17.7006 22.3821
0 5.5102 10.0276 12.8021 17.5814 22.2412
0 3.1558 7.6719 12.2234 15.1850 19.9813
0 3.1794 7.6655 12.3352 15.4489 20.0210
0 4.7771 7.5656 12.2135 17.4471 19.8625
0 4.7598 7.5804 12.2065 17.5210 20.0173];
Out = matrixVectorDist(U,[MA;TA]);
vectorName = {'MA';'TA'};
DesiredResult = table(Out(:,1),Out(:,2),Out(:,3),vectorName(Out(:,3)), 'VariableNames',{'U(row #)','Distance','Vector #','VectorName'})
DesiredResult = 10×4 table
U(row #) Distance Vector # VectorName ________ ________ ________ __________ 1 2.5091 1 {'MA'} 2 2.3509 1 {'MA'} 3 1.491 2 {'TA'} 4 1.4205 2 {'TA'} 5 1.9423 1 {'MA'} 6 1.8656 1 {'MA'} 7 3.6066 2 {'TA'} 8 3.4026 2 {'TA'} 9 2.4129 2 {'TA'} 10 2.3739 2 {'TA'}
function [distc,idx] = matrixVectorDist(M,v)
% M = Argument Matrix
% v = Comparison Vector Or Vectors
for k = 1:size(M,1)
dst(:,k) = sqrt(sum((M(k,:)-v).^2,2)); % Calculate Distances
[closest,idx] = min(dst(:,k)); % Find Closest To Elements Of 'v'
distc(k,:) = [k closest idx]; % Return Closest & 'v' Index
end
end
The ‘matrixVectorDist’ function is not robust, since it was not designed to be. It is simply designed to apply to this particular problem, and while it will work for similar problems, it is not equivalent to pdist2.
.
Nicle Davidson
Nicle Davidson 2021년 11월 12일
@Star Strider you are a star!
Star Strider
Star Strider 2021년 11월 12일
Thank you!!!
.

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

추가 답변 (0개)

카테고리

태그

질문:

2021년 11월 11일

댓글:

2021년 11월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by