Arranging the cell array in the corresponding rows.

조회 수: 1 (최근 30일)
pr
pr 2014년 5월 8일
편집: pr 2014년 5월 9일
I have a cell array which has the following format: action (a1,a2,a3,...) and followed by the corresponding "id-number" like(01,02,03,.....) i want to generate an output in which all the unique "id-number" belongs to one group.
if true
c_in = {'a1ev01','a1ev02','a2ev01','a2ev02','a3ev01','a3ev02'}; % and so on....
% output
c_out = {'a1ev01','a2ev01','a3ev01';...
'a1ev02','a2ev02','a3ev02';}; % or in another format of the same form
end
  댓글 수: 1
Cedric
Cedric 2014년 5월 8일
Is there the same number of elements for each group/ID?

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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2014년 5월 9일
c_in = {'a1ev01','a1ev02','a2ev01','a2ev02','a3ev01','a3ev02'};
ii = regexp(c_in,'\d*','match');
i1 = str2double(cat(1,ii{:}));
c_out = accumarray([i1(:,2),i1(:,1)],(1:numel(c_in))',[],@(x)c_in(x));
  댓글 수: 1
pr
pr 2014년 5월 9일
편집: pr 2014년 5월 9일
@Andrei Bobrov thanks for the help. Excellent solution

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

추가 답변 (1개)

the cyclist
the cyclist 2014년 5월 8일
Related to Cedric's question in his comment ... Is it a simple reshape?
c_out = reshape(c_in,2,[])
  댓글 수: 2
pr
pr 2014년 5월 8일
it works only in the case for "2" "id-numbers" fails in the case when ther is a 3rd "id-number" or 4th and so on.
if true
c_in = {'a1ev01','a1ev03','a2ev01','a2ev02','a3ev03','a3ev02'};
end
Cedric
Cedric 2014년 5월 8일
편집: Cedric 2014년 5월 9일
If there is always the same number of elements per group/ID and the order follows always the same schema, you (pr) can perform a reshape based on an appropriate number of rows or columns. If the second condition falls, there is a little more work but you can still store the outcome in a rectangular cell array. If both conditions fail, you must build a cell array of cell arrays.

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

카테고리

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

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by