# Get new Double Matrix from Data on a Cell Array

조회 수: 1 (최근 30일)
Maria 2014년 8월 21일
편집: Maria 2014년 8월 21일
I have a cell variable with 13000 rows and 11 columns. For instance:
C1 c2 c3 c4 c5 c6 c7
A={22 1988 1992 2001 2001 [] []
23 1983 1988 [] [] [] []
29 1983 1985 1988 1991 2000 2003}
C1 is a code for an individual; c2 is the year the individual first is in the sample; c3 is the year he/she leaves. C4 is the year the individual comes back to the sample; c5 is the year the individual goes off again; and so on. The sample period is from 1983 to 2013. I can have individuals that appear in one year, leave in other and never appear again; as others may be constantly on and off the sample. I am trying to get the following variable:
• A double variable with 3 rows, ordered by year, indicating for each year and for each individual the number of years the individual is in the sample in year x.
• If the indiviual leaves the period for at least one year and comes back again , I start the count from one (number of years the individual is in the sample).
• If the individual is less than 3 years in the sample don't consider it for the count .
B=[1983 23 1
1983 29 1
1984 23 2
1984 29 2
1985 23 3
1985 29 3
1986 23 4
1987 23 5
1988 22 1
1988 23 6
1988 29 1
1989 22 2
1989 29 2
1990 22 3
1990 29 3
1991 22 4
1991 29 4
1992 22 5
2001 29 2
2002 29 3
2003 29 4]
Can someone help me? Thank you.
##### 댓글 수: 2없음 표시없음 숨기기
Guillaume 2014년 8월 21일
The last rows of B in your example should be
2001 29 2
2002 29 3
2003 29 4]
to be consistent with the rest.
Maria 2014년 8월 21일
you are right. My mistake

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

### 채택된 답변

Guillaume 2014년 8월 21일
Something like this would do:
B = [];
for row = 1:size(A, 1)
personid=A{row, 1};
for col = 2:2:size(A, 2)
startyear = A{row, col};
endyear = A{row, col+1};
if endyear - startyear >= 2 %also works if endyear and startyear are empty
for year = startyear:endyear
B = [B; year personid year-startyear+1];
end
end
end
end
B = sortrows(B, [1 2]);
This assumes that there's always a leaving year.
The only issue with this code is the constant resizing of B. You could preallocate a huge enough array (it can't have more rows than number of persons * 30 years) and resize at the end to the actual size if it's a problem for you

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

### 카테고리

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

### Community Treasure Hunt

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

Start Hunting!

Translated by