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일

0 개 추천

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

Arif Widianto
Arif Widianto 2020년 7월 6일
Hi, Matt
Thank you for the answer. Actually it's not what I want. What I want is a label that represents a certain of order. (i.e. the order of [1,2,3] is labeled as A and [3,1,2] is labeled as B). So, if I have 3 variables there are 6 label possibilities and I can label it manually. However, actually I have a 7-by-m matrix with m is over than a 1000 which will be hard to label it manually.
dpb
dpb 2020년 7월 6일
factorial(7) ==> 5040.
What's your suggestion for labels?
Arif Widianto
Arif Widianto 2020년 7월 6일
Hi dpb,
Thank you for the response. Yes, exactly it is. I think 1-5040 would do.
Rather than creating a cell array of scalar string arrays and then converting that to a non-scalar string array:
string({"A","B","C"})
simply create the non-scalar string array right from the start:
["A","B","C"]
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개)

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

질문:

2020년 7월 5일

편집:

2020년 7월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by