How to to group each set of data in individual cell array ?

조회 수: 1 (최근 30일)
Pradya Panyainkaew
Pradya Panyainkaew 2018년 1월 13일
답변: Stephen23 2018년 1월 13일
I have a raw data matrix as
A=[111 1 2 3; 111 4 5 6; 111 7 8 9; 112 10 11 12 ; 112 13 14 15; 112 16 17 18];
the first column is a customer ID.
I want to group each customer in individual cell array as
B=1X2 cell where
B{1,1}=[111 1 2 3; 111 4 5 6; 111 7 8 9] and
B{1,2}=[112 10 11 12 ; 112 13 14 15; 112 16 17 18]
How can I create program to get a result like B ?

답변 (1개)

Stephen23
Stephen23 2018년 1월 13일
>> A = [111,1,2,3;111,4,5,6;111,7,8,9;112,10,11,12;112,13,14,15;112,16,17,18];
>> [~,~,X] = unique(A(:,1));
>> B = accumarray(X,(1:size(A,1)).',[],@(r){A(r,:)});
>> B{:}
ans =
111 1 2 3
111 4 5 6
111 7 8 9
ans =
112 10 11 12
112 13 14 15
112 16 17 18
>>

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by