How to group double matrix based on the first column values and list second values as observations?
2 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Bruno Luong
on 12 Oct 2018
Edited: Bruno Luong
on 12 Oct 2018
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
More Answers (0)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!