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.
0 个评论
采纳的回答
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 Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!