How to group double matrix based on the first column values and list second values as observations?

2 ビュー (過去 30 日間)
I have a matrix of double numbers:
X =
1 2.1
1 2.3
2 1.9
3 4.0
1 2.0
I want to group this based on the first column value and list the second values as observations:
Result = [
1 2.1 2.3 2.0
2 1.9
3 4.0
];
How can I achieve this? And is this a good way of representing data if I need to calculate their standard errors and plot both observations and error bars on a plot? Any help will be appreciated! Thanks.

採用された回答

Bruno Luong
Bruno Luong 2018 年 10 月 12 日
編集済み: Bruno Luong 2018 年 10 月 12 日
X = [1 2.1
1 2.3
2 1.9
3 4.0
1 2.0]
[id,~,j]=unique(X(:,1));
val = accumarray(j,X(:,2),[],@(x) {x.'});
s = struct('id', num2cell(id), 'val', val);
for k=1:length(s)
sk = s(k);
fprintf('id = %d, val = %s, std = %f\n', sk.id, mat2str(sk.val), std(sk.val));
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by