我有一个单元格数(data
),看起来像这样(在此处缩短):
'45.203885' '-90.600123' '119-8001' 733144 NaN
'45.203885' '-90.600123' '119-8001' 733147 NaN
'45.203885' '-90.600123' '119-8001' 733150 NaN
'45.203885' '-90.600123' '119-8001' 733153 NaN
'45.203885' '-90.600123' '119-8001' 733156 NaN
'45.203885' '-90.600123' '119-8001' 733159 NaN
我想填写NaN
的第5列,具体取决于第四列(使用datenum
转换的日期)是否匹配B
。
B
(也是一个单元格)看起来像这样(示例也很缩短了很多):
'45.203885' '-90.600123' '119-8001' 733144 '3.3'
'45.203885' '-90.600123' '119-8001' 733150 '9.5'
'45.203885' '-90.600123' '119-8001' 733156 '6.8'
您可以看到,第四列日期在B
中不会始终如一。我正在尝试将NaN
'S添加到第5列中,其中B(:,3)
和B(:, 4)
与data(:,3)
和data(:, 4)
不匹配。
最终产品应该看起来像:
'45.203885' '-90.600123' '119-8001' 733144 '3.3'
'45.203885' '-90.600123' '119-8001' 733147 NaN
'45.203885' '-90.600123' '119-8001' 733150 '9.5'
'45.203885' '-90.600123' '119-8001' 733153 NaN
'45.203885' '-90.600123' '119-8001' 733156 '6.8'
'45.203885' '-90.600123' '119-8001' 733159 NaN
如果 data
是矩阵,我只会执行以下操作:
data_ind = ismember(data(:,3:4),B(:,3:4),'rows');
,但我不知道如何使用单元格。某种形式的cellfun
会做技巧吗?
sd = size(data,1); %// number of rows of data
sb = size(B,1); %// number of rows of B
[dd bb] = ndgrid(1:sd,1:sb); %// all combinations (row of data, row of B)
cond1 = strcmp(data(dd,3),B(bb,3)); %// test col 3 for all combinations
cond2 = [data{dd,4}].'==[B{bb,4}].'; %// test col 4 for all combinations
cond = reshape(cond1 & cond2, sd, sb); %// combine the two conditions
[ib, id] = max(cond); %// id contains the index of the first 1 (if any) ...
%// ... of each col in cond; and ib is a logical index of the row of that 1
id = id(ib); %// keep only id for which the maximum is 1
data(id,:) = B(ib,:); %// copy matching rows of B into data
示例data
和B
都包含与其他变量的任何行不匹配的行:
data = {
'45.203885' '-90.600123' '119-8001' 733144 NaN
'45.203885' '-90.600123' '119-8001' 733147 NaN
'45.203885' '-90.600123' '119-8001' 733150 NaN
'45.203885' '-90.600123' '119-8001' 733153 NaN
'45.203885' '-90.600123' '119-8001' 733156 NaN
'45.203885' '-90.600123' '119-8001' 733159 NaN};
B = {
'45.203885' '-90.600123' '119-8001' [733144] '3.3'
'45.203885' '-90.600123' '119-8001' [733150] '9.5'
'45.203885' '-90.600123' '119-8001' [733156] '6.8'
'45.203885' '-90.600123' '169-8001' [833156] '6.8'};
结果:
data =
'45.203885' '-90.600123' '119-8001' [733144] '3.3'
'45.203885' '-90.600123' '119-8001' [733147] [NaN]
'45.203885' '-90.600123' '119-8001' [733150] '9.5'
'45.203885' '-90.600123' '119-8001' [733153] [NaN]
'45.203885' '-90.600123' '119-8001' [733156] '6.8'
'45.203885' '-90.600123' '119-8001' [733159] [NaN]
目前尚不清楚您对第三列做什么,因为所有条目都是相同的。另外,您的问题有些困惑,因为您想要data
中的B
还是B
中的data
。可能最快,最直接的方法是使用for
循环:
data = {'45.203885' '-90.600123' '119-8001' 733144 NaN
'45.203885' '-90.600123' '119-8001' 733147 NaN
'45.203885' '-90.600123' '119-8001' 733150 NaN
'45.203885' '-90.600123' '119-8001' 733153 NaN
'45.203885' '-90.600123' '119-8001' 733156 NaN
'45.203885' '-90.600123' '119-8001' 733159 NaN};
B = {'45.203885' '-90.600123' '119-8001' 733144 '3.3'
'45.203885' '-90.600123' '119-8001' 733150 '9.5'
'45.203885' '-90.600123' '119-8001' 733156 '6.8'};
d3 = data(:,3);
d4 = [data{:,4}].';
for i = 1:size(B,1)
data(strcmp(d3,B{i,3})&d4==B{i,4},5) = B(i,5);
end
不要害怕使用for
循环。您也可以使用cellfun
进行操作,但是它需要使用eval
。
,由于您只有唯一的ID Datenum组合,因此您可以执行以下操作:
data_ind = ismember(strcat(data(:,3),num2str([data{:,4}]')),...
strcat(B(:,3),num2str([B{:,4}]')));
这所做的是将第三列和第4列加入到一个字符串中,例如
'45.203885' '-90.600123' '119-8001' 733144 NaN
'45.203885' '-90.600123' '119-8001' 733147 NaN
将成为
'119-8001733144'
'119-8001733147'
等等。然后,它将这些字符串从data
矩阵与B
矩阵进行比较,从而为您提供了索引矩阵。