Take elements from a cell variable to other based on a condition

조회 수: 1 (최근 30일)
Maria
Maria 2014년 8월 11일
댓글: Maria 2014년 8월 11일
I have a cell type variable A with 8 rows and 500 columns:
code1 year value code1 *code2*
A={ 1037 1999 50,2 1037 110 [] [] 1
1062 1999 50,2 1062 *[]* [] [] 0
1242 1999 58,4 1242 *[]* [] [] 0
1255 1999 56,2 1255 881 [] [] 1}
And a cell type variable B with 21 rows and 10000 columns:
x 1994 1995 1996 1997 1998 1999 2000 2001 ...
B= {1037 110 110 110 110 110 110 873 873
1039 721 721 1242 [] [] [] [] []
1050 495 495 495 495 829 282 282 []
1062 228 228 228 282 282 *282* [] []
1242 86 86 86 86 86 *86* [] []
1249 118 37 37 37 37 37 37 []
1254 7 7 7 7 7 7 157 157
1255 37 37 37 37 37 881 16 16}
code2 is missing in some rows of variable A.
I would like to complete the 5th column of A (code2) by going to the 7th column of B(1999) and if A(:,1) and B(:,1)match ( code1 and 'x' ), place that value in A.
My new A would be:
code1 year value code1 *code2*
ANEW={ 1037 1999 50,2 1037 110 [] [] 1
1062 1999 50,2 1062 *282* [] [] 0
1242 1999 58,4 1242 *86* [] [] 0
1255 1999 56,2 1255 881 [] [] 1}
Can someone help me? Thank you.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 8월 11일
편집: Azzi Abdelmalek 2014년 8월 11일
ic5=find(cellfun(@isempty,A(:,5) ))
for k=ic5'
ib1=find(cell2mat(B(:,1))==A{k,1})
if ~isempty(ib1)
A(k,5)=B(ib1,7)
end
end

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by