sorting according to another vector

218 ビュー (過去 30 日間)
Danielle Leblanc
Danielle Leblanc 2011 年 8 月 5 日
コメント済み: Walter Roberson 2022 年 2 月 10 日
Hi,
I have a vector A that doesn't follow any order. I want to sort Matrix B that has the first column of values similar to those of A but in different order. Is it possible to sort B according to A using a single function or should I write many codes to do so?

採用された回答

Walter Roberson
Walter Roberson 2011 年 8 月 5 日
Sorry you will have to use multiple calls:
[a_sorted, a_order] = sort(A);
newB = B(a_order,:);
  1 件のコメント
Jan
Jan 2011 年 8 月 5 日
@Danielle: Ok, then I have misunderstood your question. The information that B has "value similar to those of A" is meaningless then - correct?

サインインしてコメントする。

その他の回答 (4 件)

Jan
Jan 2011 年 8 月 5 日
I'm not sure, if I understand the question correctly. Do you want to bring B in the same order as the unsorted A? Then either sort both vectors and mix their sorting indicies:
[As, Ai] = sort(A);
[Bs, Bi] = sort(B(:, 1));
ABi(Ai) = Bi;
Now ABi is the sorting index to bring B to the order of A:
isequal(A, B(ABi, 1)) % ==> 1
Or let ISMEMBER do this for you:
[dummy, order] = ismember(A, B(:, 1));
isequal(A, B(order, 1)) % ==> 1

Pramit Biswas
Pramit Biswas 2018 年 1 月 31 日
編集済み: Pramit Biswas 2018 年 1 月 31 日
function B = sortBlikeA(A,B)
[~, Ao] = sort(A);
Bs=sort(B);
B(Ao)=Bs;
end
  1 件のコメント
Marco Schnizer
Marco Schnizer 2022 年 2 月 10 日
Nice Code, thank you!

サインインしてコメントする。


inzamam shoukat
inzamam shoukat 2018 年 10 月 26 日
編集済み: inzamam shoukat 2018 年 10 月 26 日
Hi every one......i have matrix lets suppose [3 2;1 9]
what i want to do is sort out the only first column in ascending order but problem is how to do it so that corresponding values which is in first row 2 for 3 in second row 9 for 1 change their positions according to the position of values of first column being sorted i.e [1 9; 3 2]...
LIMITATION : sorting operation never applied to second column....
  2 件のコメント
madhan ravi
madhan ravi 2018 年 10 月 26 日
Please ask a separate question
Steven Lord
Steven Lord 2018 年 10 月 26 日
Use sortrows.

サインインしてコメントする。


Marco Bertola
Marco Bertola 2021 年 2 月 14 日
編集済み: Marco Bertola 2021 年 2 月 14 日
I think the question (which I had too) was:
I have a reference vector [1.2 3.3 4.4] eg
and another ``similar'' vector [3.2 1.1 4.4]
Problem; find the optimal sorting of vector 2 so that it is ''closest'' (L^2 norm eg) to the reference vector.
Say X is your reference vector of size (1,n) and Xnew is the new vector of the same size
then what you want is accomplished like so:
dist = abs(ones(n,1)*X - Xnew.' ones(1,n));
[t1,J]= min(dist);
%now J has the optimal permutation%
Xnew=Xnew[J]
  1 件のコメント
Walter Roberson
Walter Roberson 2022 年 2 月 10 日
If you are not permitted to duplicate any elements, and the entries are all finite then
[~, refidx] = sort(reference_vector);
sorted_similar = sort(similar_vector);
new_similar = sorted_similar(refidx);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by