Substitute blank entries by 1 or 0 in a column of a cell given a condition.
1 次查看(过去 30 天)
显示 更早的评论
I have a double variable A with 3 columns and 90000 rows, sorted first by a1(year) and then by a2 (code); and a cell variable B with 10 columns and about 3000 rows, sorted by b2 (year) and then by b1 (code). For example:
a1 a2 a3
A=[2001 7 3
2001 55 2
2002 9 4
2002 12 1
2002 24 3
2003 13 5…]
b1 b2 b3 b4 b5 b6 b7 b8 b9 b10
B={7 2001 28,14 [] 1388 33 [] [] [] []
8 2002 44,44 [] 150 [] [] [] [] []
12 2002 54,46 [] 100 21 49 40701 [] []
13 2003 46,41 [] 189 22 44 30901 [] []..}.
If a1 matches b2 (years) and a2 matches b1 (codes), I would like to substitute the blank entries in b9 of B by 1 if the value of c3 is higher than 1 and zero if the value of c3 is 1. So my B would then be:
b1 b2 b3 b4 b5 b6 b7 b8 b9 b10
B={7 2001 28,14 [] 1388 33 [] [] 1 []
8 2002 44,44 [] 150 [] [] [] [] []
12 2002 54,46 [] 100 21 49 40701 0 []
13 2003 46,41 [] 189 22 44 30901 1 []..}.
Could someone help me? Thank you.
2 个评论
采纳的回答
Andrei Bobrov
2014-8-21
b1 = cell2mat(B(:,[2 1]));
[i0,i1] = ismember(b1,A(:,1:2),'rows');
B(i0,10) = num2cell(A(i1(i0),3) > 1);
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!