# Sorting a matrix column to match another column

3 ビュー (過去 30 日間)
Keerthi Krishna 2022 年 11 月 28 日

I have a 20x3 table and a 20x6 double.
The table has a bunch of strings and a column of identifying numbers in no particular order.
example:
[a b 44;
c d 16;
e f 03;
g h 63];
A column of the other matrix has the exact same numbers in a different order.
example:
[63;
16;
44;
3];
My question is if there's any way to sort the data in the table to match the order of the other matrix.
example:
[g h 63;
c d 16;
a b 44;
e f 03];
##### 1 件のコメント表示非表示 なし
Stephen23 2022 年 11 月 28 日
Use the second output of ISMEMBER.

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

### 回答 (1 件)

Voss 2022 年 11 月 28 日
t = table( ...
["a";"c";"e";"g"],["b";"d";"f";"h"],[44; 16; 03; 63])
t = 4×3 table
Var1 Var2 Var3 ____ ____ ____ "a" "b" 44 "c" "d" 16 "e" "f" 3 "g" "h" 63
d = [63; 16; 44; 3];
[~,idx] = ismember(d,t{:,3});
t_sorted = t(idx,:)
t_sorted = 4×3 table
Var1 Var2 Var3 ____ ____ ____ "g" "h" 63 "c" "d" 16 "a" "b" 44 "e" "f" 3

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

### カテゴリ

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by