if I have
A=[1 2; 2 4; 3 4; 4 1; 5 1; 6 4; 7 4; 8 2; 9 1; 10 1]
where 1st column represents customer ID and 2nd column shows Clustering Number. How can I group customer ID into a same cluster such as
cluster1=[4 5 9 10], cluster2=[1,8], cluster3=[ ], cluster4=[2,3,6,7]

 채택된 답변

ANKUR KUMAR
ANKUR KUMAR 2018년 1월 8일
편집: ANKUR KUMAR 2018년 1월 8일

1 개 추천

A=[1 2; 2 4; 3 4; 4 1; 5 1; 6 4; 7 4; 8 2; 9 1; 10 1]
id=unique(A(:,2))
for ii=1:length(id)
iid{ii}=A(find(A(:,2)==id(ii)),1)
end
you can see the outputs just by entering iid{1}

댓글 수: 3

Guillaume
Guillaume 2018년 1월 8일
편집: Guillaume 2018년 1월 8일
Never mind that this answer is incorrect (try it when the customer id is not equal to the row number such as A = [1234 1;4567 1]), find is usually unnecessary.
If you wanted to use a loop, the body of the loop should be:
for ii = 1 : numel(id)
iid{ii} = A(A(:, 2) == id(ii), 1)
end
Also, you should preallocate the cell array rather than growing it in the loop, wasting time reallocating a new cell array at each step. So before the loop:
iid = cell(numel(id), 1);
for ...
Or use accumarray which avoids all these issues...
edit: I see the answer has been edited to correct the bug so the answer now works. find is still unnecessary. It just slows the code for no benefit.
ANKUR KUMAR
ANKUR KUMAR 2018년 1월 8일
Sir, I am just a student. That was my mistake. Sorry for this.

It's fine. Don't worry. I'm a rookie also. It's not your fault. Let's share.

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

추가 답변 (1개)

Guillaume
Guillaume 2018년 1월 8일

1 개 추천

Assuming your clustering numbers are all strictly positive integers:
accumarray(A(:, 2), A(:, 1), [], @(custs) {custs})

카테고리

도움말 센터File Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by