extract data with unique ID

3 ビュー (過去 30 日間)
sensation
sensation 2019 年 10 月 30 日
コメント済み: sensation 2019 年 10 月 31 日
Hi,
I have already read a lot regarding accumarray and examples but still struggling with this one.
I have data of 1500*19 columns where in the column e.g. 14 are data that I want to use as a unique ID in order to extract all other columns as a separate arrays with the same unique ID.
my example where unique id is in column 1:
subs = [1 1 5 6 ;2 1 4 5;3 1 4 5;1 1 2 5;2 1 5 8;4 1 4 8;2 1 7 8;4 1 5 4;2 1 2 2;1 2 5 5] % data
subs_unique=unique(subs(:,1)) % identifying the unique id in column 1 but it can also be column 14
A = accumarray(subs,subs_unique,[],@(x) {x}) % gives an error
desired result:
A{1}= [1 1 5 6;1 1 2 5;1 2 5 5]
A{2}=[2 1 4 5,2 1 5 8;2 1 7 8;2 1 2 2] and so on
Thansk a lot,
Cheers,
Mark

採用された回答

Daniel M
Daniel M 2019 年 10 月 30 日
As Walter also suggested:
[~,~,locGroups] = unique(subs(:,1));
% also look at [~,~,locGroups] = unique(subs(:,1),'stable');
A = splitapply(@(v) {v}, subs, locGroups);
  1 件のコメント
sensation
sensation 2019 年 10 月 31 日
Thanks a lot to both of you!
Cheers
M.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 10 月 30 日
Take the third output of unique() instead of the first. Or use findgroups()
splitapply() can also be used to do the splitting.

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by