How to divide a matrix and find the index?
1 次查看(过去 30 天)
显示 更早的评论
Hi there, I have a matrix such as a = ['hello' 'hi' 'bye';1 3 5], i want to use the 2nd column to categorize the matrix, but what i have in mind is:
x = a(a(:,2)==1,:); y = a(a(:,2)==3,:); z = a(a(:,2)==5,:);
but when there are many categorizations, i think this method can be very dumb and long, i wonder whether there is a simple way or function to deal with this?
PS: i know histc can categorize a vector and return a index matrix, but the index histc returns cannot be used to find the original index of the original matrix.
0 个评论
采纳的回答
Walter Roberson
2015-5-19
[uvals, ia, ic] = unique(a(:,2));
categorized = accumarray(ic, (1:size(a,1))', [], @(v) {a(v,:)});
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!