Finding highest position of values in two arrays?
Mostrar comentarios más antiguos
Hello,
i have two matrices with two columns. I want to sort them after one column. After it this matrices should compare one column. For example after the sort of the first row the first row of A should be compared with the first row of B. In this both rows are the same numbers, but in different sequence. The highest number in both of this rows should be copied to a matrix C(1,1). The date from the second row of A to C(2,1) and from B to C(3,1). This should be done for all members of the A matrix.
How could the code look like?
Respuestas (3)
Update: the code below incorporates what you describe and results in the required C.
A=[0.1 3;0.3 1;0.6 2];B=[0.7 2; 0.8 3; 0.9 1];
%first, sort by ID, which makes everything easier
A = sortrows(A, 2);
B = sortrows(B, 2);
%next, find the rank for each ID
[~,rankA]=sort(A(:,1),'descend');
[~,rankB]=sort(B(:,1),'ascend');
%calculate the cost and sort it to get the overall ranking
costFcn=rankA/(numel(rankA)-0.1)+rankB/numel(rankB);
[~,rankC]=sort(costFcn);
%create C
C=[rankC A(rankC,1) B(rankC,1)];
original post:
Something like this?
dummy_A=rand(10,2);
dummy_B=rand(10,2);
[~,order]=sort(dummy_A(:,1));
A=dummy_A(order,:);
[~,order]=sort(dummy_B(:,1));
B=dummy_B(order,:);
C=[max([A B],[],2) A(:,2) B(:,2)];
If this is not what you mean, please provide some way to generate plausible data and a sample output.
15 comentarios
Michael Simonovski
el 21 de Jul. de 2018
Rik
el 22 de Jul. de 2018
Did you make a mistake in you last step? Because I don't understand how you want to make the first column. The code below will get you most of the way there though.
E.g. the second row: C is [1 .3 .8], so the value from A is taken, so apparently we are looking for the minimum. Then the third row would need to be [3 .1 .9], but according to you, it should be .8? Where does that value come from?
A=[0.1 3;0.3 1;0.6 2];B=[0.7 2; 0.8 3; 0.9 1];
%sort descending
[~,order]=sort(A(:,1),'descend');
A=A(order,:);
[~,order]=sort(B(:,1),'ascend');
B=B(order,:);
%find the maximum value and its index
AB_col1=[A(:,1) B(:,1)];
AB_col2=[A(:,2) B(:,2)];
[~,minidx]=min(AB_col1,[],2);
minind=sub2ind(size(AB_col2),1:numel(minidx),minidx');
C=[AB_col2(minind)' AB_col1];
Image Analyst
el 22 de Jul. de 2018
You say "The script should sort the both matrix after the first row. The matrix A should be: A=[0.6 2; 0.3 1;0.1 3] " but clearly row 1 has moved, so you're not sorting AFTER the first row (in other words, just sorting rows 2 and 3) since row 1 is being included in the sorting. So what do you mean? Do you want to sort the whole A array, in ascending or descending order, by some column? If so, use sortrows().
Michael Simonovski
el 22 de Jul. de 2018
Editada: Michael Simonovski
el 22 de Jul. de 2018
Image Analyst
el 22 de Jul. de 2018
What's the use case for this? Can you give us some context as to why you need to do these crazy index gymnastics?
Rik
el 22 de Jul. de 2018
Trying to answer your question seems to be a moving target. It might indeed be a better idea to explain what you're trying to do, so we can help you with the underlying task.
Michael Simonovski
el 22 de Jul. de 2018
Image Analyst
el 23 de Jul. de 2018
Is the status the force? And the integer ID the row? So do you have a structure array, or an N by 2 matrix where the row is the ID, the first column is the coefficient of friction, and the second column is the force? So if the force is the status and you want the highest status, just use max() to get the ID of the item with the best status (highest force):
[maxValue, IdOfItemWithBestStatus] = max(yourMatrix(:, 2))
Michael Simonovski
el 23 de Jul. de 2018
Rik
el 23 de Jul. de 2018
You need to have an explicit definition of what is better. A possible cost function would be to sum the two ranks. Or maybe you value one parameter more than the other, so you want to square one of them, or multiply one with a scalar.
Michael Simonovski
el 23 de Jul. de 2018
Guillaume
el 23 de Jul. de 2018
@Rik,
[~,order]=sort(A(:,2),'ascend');
A=A(order,:);
is more simply:
A = sortrows(A, 2);
Rik
el 23 de Jul. de 2018
Good point. I always forget that rowfun only works on tables, but sortrows works on matrices as well.
Michael Simonovski
el 23 de Jul. de 2018
Rik
el 23 de Jul. de 2018
Doesn't my updated answer do exactly what you describe?
Michael Simonovski
el 23 de Jul. de 2018
3 comentarios
Guillaume
el 23 de Jul. de 2018
a) Don't use answers for comments.
b) it doesn't work is a completely useless statement. We're not mind reader. If it doesn't work because you get an error, then what is the error? If it doesn't work because it produces a result different from what you expected, then what result did you get and what did you expect? If it doesn't work for some other reason, then explain.
Michael Simonovski
el 23 de Jul. de 2018
Rik
el 27 de Jul. de 2018
I'm on mobile, so I can't test my code with this input, but it should yield the correct result. Does it? Otherwise I would suggest using Guillaume's answer.
Guillaume
el 24 de Jul. de 2018
I don't really understand your description of the rule to figure out the ordering. Whatever it is however, it should be trivial to implement.
I would recommend that you switch to using tables, it would allow you to trivially merge your two arrays and sort the result according to whichever rule you want. In addition, it makes clear what each column represent and also makes whatever calculation you want to perform easier to understand
A=[0.1,2;0.6,9;0.7,10;0.4,6;0.8,3;0.9,1];
B=[0.3,1;0.8,10;0.6,3;0.9,6;0.22,9;0.82,2];
%convert arrays to tables
tA = array2table(A, 'VariableNames', {'Friction', 'ID'});
tB = array2table(B, 'VariableNames', {'Force', 'ID'});
%merge tables
tmerged = join(tA, tB)
%create a cost function. Use whatever formula you want
tmerged.Cost = tmerged.Force + tMerged.Friction %e.g sum of force and friction
%sort first according to cost, then if equal according to force, then friction
tsorted = sortrows(tmerged, {'Cost', 'Force', 'Friction'})
Categorías
Más información sobre Shifting and Sorting Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!