How to divide a matrix and find the index?

조회 수: 1 (최근 30일)
Weiqian Jiang
Weiqian Jiang 2015년 5월 19일
댓글: Weiqian Jiang 2015년 5월 19일
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.

채택된 답변

Walter Roberson
Walter Roberson 2015년 5월 19일
[uvals, ia, ic] = unique(a(:,2));
categorized = accumarray(ic, (1:size(a,1))', [], @(v) {a(v,:)});
  댓글 수: 2
Weiqian Jiang
Weiqian Jiang 2015년 5월 19일
thanks for your help, i've helped about accumarray and find it useful in solving my problems, although it might be difficult to understand.. thanks again!
Weiqian Jiang
Weiqian Jiang 2015년 5월 19일
maybe histc and arrayfun can be easier to handle this problem [n bin] = histc(a(:,2),low:step:high) groups = low:step:high arrayfun(@(x)a(bin==x,:),groups,'uniform',0);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by