Sorting a cell array of string, based on the order of another

14 Ansichten (letzte 30 Tage)
Sébastien
Sébastien am 22 Feb. 2011
Kommentiert: Vina B am 11 Aug. 2016
I have 2 cell array of string. The content of the cells is exactly the same, only the order is different.
I want to sort one, without changing the order of the other.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 23 Feb. 2011
[tf, idx] = ismember(B,A);
Places where tf are false are places where B is not in A. And of course, B might not cover all of A. If you know that B covers exactly A, then B(idx) should be the same as A

Weitere Antworten (3)

Sébastien
Sébastien am 23 Feb. 2011
ismember() is exactly what I needed. Thank you very much!

Walter Roberson
Walter Roberson am 22 Feb. 2011
Your title and your description do not agree. Your description has a trivial answer ("just don't use the second variable") so I will answer based on your title;
[sortedvals, sortidx] = sort(FirstCell);
newCell = SecondCell(sortidx);

Sébastien
Sébastien am 23 Feb. 2011
No they agree, I've not been clear. I've :
Vector A {'pierre'} {'paul'} {'jacques'}
Vector B {'jacques'} {'pierre'} {'paul'}
I want to get the index on how to reorder B to fit the order of A.
A first solution would be to sort both alphabetically, I could do that, but here the point is that I don't want to reorder A.

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by