How to label sorted variables

7 ビュー (過去 30 日間)
Arif Widianto
Arif Widianto 2020 年 7 月 5 日
編集済み: Matt J 2020 年 7 月 6 日
Hi,
Let say I have a matrix obtained from sort function,
x = [1,1,3;
2,2,1;
3,3,2]
with 1, 2, and 3 represent sorted values of certain variables in vertical manner alongside each column. I want to have another variable that stores the label of corresponding order.
% (e.g. It will be like this)
y = [A,A,B]
How can I do this if I have 7 variables that I want to sort in a 7-by-m matrix?
Thank you.
  1 件のコメント
dpb
dpb 2020 年 7 月 5 日
Save the optional second index array variable and use it to reference the label array.

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

採用された回答

Matt J
Matt J 2020 年 7 月 5 日
Maybe what you want:
>> labels=string({"A","B","C"});
>> x = [1,1,3;
2,2,1;
3,3,2];
>> labels(x)
ans =
3×3 string array
"A" "A" "C"
"B" "B" "A"
"C" "C" "B"
  6 件のコメント
dpb
dpb 2020 年 7 月 6 日
You'll have to save the perms(1:7) array and find the location of each column in it as the index/label, then.
A string match will probably be faster than all(ismember()) or the like.
Matt J
Matt J 2020 年 7 月 6 日
編集済み: Matt J 2020 年 7 月 6 日
One thing you could do is create a label look-up table LUT, maintained a s a cell array
LUT=cell(1,2.^7);
vals=perms(1:7)*2.^(0:6).';
LUT(vals)=labels;
and now for a 7x3000 data set
[~,x]=sort(rand(7,3000)); %fake data
you would simply do
y=LUT(2.^(0:6) * x); %perform the label look-up

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

その他の回答 (0 件)

カテゴリ

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