How to transform data from rows to columns, based on a specific column

Currently i have a cell array containing a batch ID in de 1st column, and a position in the 2nd column. An example is shown below.
Batch Pos
001 1
001 3
001 8
002 1
003 5
003 8
007 2
007 4
007 7
007 8
011 6
013 2
014 1
014 3
014 5
014 6
What I am trying to achieve is to have only 1 row per batch ID, and convert the pos to seperate column with either True or 1. The pos will always be a number between 1 and 8.
Batch Pos 1 Pos 2 Pos 3 Pos 4 Pos 5 Pos 6 Pos 7 Pos 8
001 TRUE TRUE TRUE
002 TRUE
003 TRUE TRUE
007 TRUE TRUE TRUE TRUE
011 TRUE
013 TRUE
014 TRUE TRUE TRUE TRUE
Can anyone help me in the right direction how to achieve this, since i am really struggling with this.

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 2월 20일
편집: Azzi Abdelmalek 2014년 2월 20일
A={'001' 1
'001' 3
'001' 8
'002' 1
'003' 5
'003' 8
'007' 2
'007' 4
'007' 7
'007' 8
'011' 6
'013' 2
'014' 1
'014' 3
'014' 5
'014' 6}
c1=A(:,1)
c2=cell2mat(A(:,2));
n=max(c2);
[ii,jj,kk]=unique(c1,'stable');
t=zeros(numel(ii),n);
for k=1:numel(ii)
idx=c2(kk==k);
t(k,idx)=1;
end
s=genvarname(repmat({'pos'},1,n+1));
s(1)={'patch'};
out=[[s;[ii num2cell(t)]]]

댓글 수: 1

That is exactly what I was looking for. Thank you Azzi Abdelmalek!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Christmas / Winter에 대해 자세히 알아보기

질문:

2014년 2월 20일

댓글:

2014년 2월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by