Finding highest position of values in two arrays?

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)

Rik
Rik el 21 de Jul. de 2018
Editada: Rik el 23 de Jul. de 2018
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

For example A=[0.1 3;0.3 1;0.6 2] and B=[0.7 2; 0.8 3; 0.9 1]
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]
The matrix B should be sort so that the smallest value (0.7) should be on top. After the sorting the both second colomns should be compared. The aim is to find the number, which has the highest position in both of the sorted colomns.
For example:
A=[0.6 2; 0.3 1;0.1 3] B=[0.7 2;0.8 3;0.9 1]
The Matrix C should be then: C=[2 0.6 0.7; 1 0.3 0.8; 3 0.1 0.8]
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
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
Michael Simonovski el 22 de Jul. de 2018
Editada: Michael Simonovski el 22 de Jul. de 2018
Yes you are right, i made a mistake it should be C=[2 0.6 0.7; 1 0.3 0.9; 3 0.1 0.8]
It should compare the both coloumns with integers and find out, which integer has the highest position in both coloumns. For example if 1 is on position A(2,2) and B(3,2) it has to be higher in C as the number 2, which is on position A(1,2) and B(7,2), because of the low position in B!
Image Analyst
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
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.
I have ten items, which are in motion. Every integer is the ID of one of this items. The other values are the friction coeff and the force, which describe the actual status of each item. I want to find which item has the best status!
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))
I want to get the ID of the Item, which has the best force and coefficient at the same time. Some items could have a better force and a bad coeff, another vice vertha. The aim is to get the item, which has both at the best status from all!
Rik
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.
I thought about to make a sum of the position of every ID and compare the sums. If two integers have the same sum, take the integer, which have the lower position in A. But i do not know how to make it in a script.
@Rik,
[~,order]=sort(A(:,2),'ascend');
A=A(order,:);
is more simply:
A = sortrows(A, 2);
Rik
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.
I do not understand how i can make it?
Rik
Rik el 23 de Jul. de 2018
Doesn't my updated answer do exactly what you describe?

Iniciar sesión para comentar.

I have tried it, but it doesnt work, why?
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];
a=A(:,2);
b=B(:,2);
number = unique([a; b]);
n = 1;
while isempty(number) ~= 1
for k = 1 : length(number)
summe(k) = find(a == number(k)) + find(b == number(k));
end
C(n,1) = number(find(summe == min(summe),1));
avalue=find(a==C(n,1));
bvalue=find(b==C(n,1));
C(n,2)=A(avalue,1);
C(n,3)=A(bvalue,1);
A(avalue,:)=[];
B(bvalue,:)=[];
n=n+1;
clear summe;
clear number;
a=A(:,2);
b=B(:,2);
number = unique([a; b]);
end

3 comentarios

Guillaume
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.
a) I didnt know it!
b) My result is:
C =
10.0000 0.7000 0.6000
1.0000 0.9000 0.1000
2.0000 0.1000 0.8000
3.0000 0.8000 0.6000
6.0000 0.4000 0.6000
9.0000 0.6000 0.6000
First problem is, that the value 9 in the first row should be at the position 2 - 4, because it has the same sum value. The second problem is, that the values in the another values do not fit (See A and B).
3) I would like to implement a step, which should make following:
If the sum of more than one value is the same, compare the values of the first row, which have the smallest. If more than one value still have the same values, compare the values of the first coloumn of matrix B?
Rik
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.

Iniciar sesión para comentar.

Guillaume
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

Etiquetas

Preguntada:

el 21 de Jul. de 2018

Comentada:

Rik
el 27 de Jul. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by