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

groupobservationsplotstandard error

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.

Best Answer

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