Match different sized cell arrays in Matlab
1 次查看(过去 30 天)
显示 更早的评论
RECCELL is a cell array with 8 columns and 30000 rows:
C1 C2 C3 C4 C5 C6 C7 C8
'AA' 1997 19970102 1 'BACHE' 'MORI' 148 127
'AA' 1997 19970108 2 'MORGAN' [] 1595 0
'AA' 1997 19970224 3 'KEMSEC' 'FATHI' 1315 297
CONCELL is a cell array with 4 columns and 70000 rows:
C1 C2 D3 D4
'AA' 1997 19970116 2,75
'AA' 1997 19970220 2,71
'AA' 1997 19970320 2,61
I would like to add to RECCELL the 4 columns of CONCELL only in case the C1s match and C3 and D3 (both dates) are the closest possible. For instance I would get in this example:
C1 C2 C3 C4 C5 C6 C7 C8 C1 C2 D3 D4
'AA' 1997 19970102 1 'BACHE' 'MORI' 148 127 'AA' 1997 19970116 2,75
'AA' 1997 19970108 2 'MORGAN' [] 1595 0 'AA' 1997 19970116 2,75
'AA' 1997 19970113 3 'KEMSEC' 'FATHI' 1315 297 'AA' 1997 19970220 2,71
- To the first row of RECCELL corresponds the first row of CONCELL.
- To the second row of RECCELL corresponds the first row of CONCELL.
- To the third row of RECCELL corresponds the second row of CONCELL.
The code I have so far is:
[~, indCon, indREC] = intersect(CONCELL(:,1), RECCELL(:,1));
REC_CON=[RECCELL(indREC,:),CONCELL(indCon,:)];
NO_REC_CON= RECCELL(setdiff(1:size(RECCELL,1), indREC),:);
It's wrong because I cannot use intersect for a string element and because I am not considering the second condition, which is to choose the closest dates.
Can someone help me? Thank you
0 个评论
采纳的回答
Geoff Hayes
2014-7-21
Maria - are there four or five columns in CONCELL? When you match on a column, do you copy over all but the first
You could do something like the following: iterate through each of the elements in RECCELL and look for those from CONCELL that match on the first column. Then find that row of CONCELL whose date is closest to that for RECCELL
[mr,mc] = size(RECCELL);
[~,nc] = size(CONCELL);
% append empty cells to RECCELL that may be populated with matching data
RECCELL = [RECCELL cell(mr,nc)];
for k=1:size(RECCELL,1)
% get the indices from CONCELL whose first column matches that of the
% first column of the kth RECCELL row
[idcs] = find(strcmpi(CONCELL(:,1),RECCELL{k,1}));
if ~isempty(idcs)
% find the two dates that are closest
dateDiff = abs(cell2mat(CONCELL(idcs,3))-cell2mat(RECCELL(k,3)));
% find the minimum
[~,minIdcs] = min(dateDiff);
% just grab the first index (in case multiple with same difference)
minIdx = minIdcs(1);
% append the data
for u=1:nc
RECCELL{k,mc+u} = CONCELL{minIdx,u};
end
end
end
Try the above and see what happens!
2 个评论
Geoff Hayes
2014-7-21
Glad that it worked, Maria. I wasn't sure about the last column in CONCELL since it appeared to be a comma separated list of two numbers (for example 2,75).
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Phased Array Design and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!