Match different sized cell arrays in Matlab

1 回表示 (過去 30 日間)
Maria
Maria 2014 年 7 月 21 日
コメント済み: Geoff Hayes 2014 年 7 月 21 日
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

採用された回答

Geoff Hayes
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 件のコメント
Maria
Maria 2014 年 7 月 21 日
編集済み: Maria 2014 年 7 月 21 日
Geoff the code worked perfectly! Really really good.Just to answer your initial question, CONCELL has 4 columns, and I gave a title to each column in the example, but in my variable in Matlab the first row entitled here as C1, C2, C3 and so on does not exist, so there's no prob. Not sure if that was the question. :) Thank you very much.
Geoff Hayes
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 ExchangePhased Array Design and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by