# Match different sized cell arrays in Matlab

조회 수: 6 (최근 30일)
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 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 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 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).

댓글을 달려면 로그인하십시오.

### 카테고리

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

### Community Treasure Hunt

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

Start Hunting!

Translated by