필터 지우기
필터 지우기

Get new variable with elements from a double matrix & cell array given a specific condition

조회 수: 1 (최근 30일)
I am working with Matlab, and I have a cell array with 5 columns and half million rows and a double matrix with columns and about a quarter million rows. Let’s call them A and B. A has both string and number elements.
There is one element that is common in both cells, although the number of rows and order is not equal. What I would like is to obtain a new variable (double or cell) with X columns from the cell array A and double matrix B every time a condition is verified. The condition would be if column Y (from A) and Z (from B) match. For instance: Cell array A:
'19970102' '00000127' 'MORRISON' '00148' 'BA'
'19970102' '00010121' 'GALLITANO A' '00001382' 'ALEX'
'19970102' '00031783' 'HOPSON' '00039' 'EDW'
'19970102' '00040486' 'STRUM' '00003' 'CHI'
'19970102' '00003337' 'KATICA H' '00001929' 'RAYMOND'
Double Matrix B:
19970101 31783 183
19970101 127 235
19970101 18290 183
19970101 835 01
19970101 40486 1976
What I would like to get in the ‘new’ variable:
19970101 31783 183 '19970102' '00031783' 'HOPSON' '00039' 'EDW'
19970101 127 235 '19970102' '00000127' 'MORRISON' '00148' 'BA'
19970101 18290 183
19970101 835 01
19970101 40486 1976 '19970102' '00040486' 'STRUM' '00003' 'CHI'
Can anyone help me? Thanks a lot for your availability.

채택된 답변

Sven
Sven 2014년 5월 27일
편집: Sven 2014년 5월 27일
Hi Maria,
This code should do what you're looking for:
A = {
'19970102' '00000127' 'MORRISON' '00148' 'BA'
'19970102' '00010121' 'GALLITANO A' '00001382' 'ALEX'
'19970102' '00031783' 'HOPSON' '00039' 'EDW'
'19970102' '00040486' 'STRUM' '00003' 'CHI'
'19970102' '00003337' 'KATICA H' '00001929' 'RAYMOND'}
B = [
19970101 31783 183
19970101 127 235
19970101 18290 183
19970101 835 01
19970101 40486 1976]
A_id = cellfun(@str2double, A(:,2))
B_id = B(:,2)
[TF, rowNosOfA] = ismember(B_id, A_id)
JOINED_B = num2cell(B); % Start with B in cell form
JOINED_B{1,end+size(A,2)} = []; % Make room for columns from A
JOINED_B(TF, size(B,2)+1:end) = A(rowNosOfA(TF),:)
JOINED_B =
[19970101] [31783] [ 183] '19970102' '00031783' 'HOPSON' '00039' 'EDW'
[19970101] [ 127] [ 235] '19970102' '00000127' 'MORRISON' '00148' 'BA'
[19970101] [18290] [ 183] [] [] [] [] []
[19970101] [ 835] [ 1] [] [] [] [] []
[19970101] [40486] [1976] '19970102' '00040486' 'STRUM' '00003' 'CHI'
Did that help you out? Note that ismember only returns the first instance where a row from B matched a row from A, but for the data you provided this is valid as there's only one match anyway.
Thanks, Sven.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by