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

3 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Sven
Sven el 27 de Mayo de 2014
Editada: Sven el 27 de Mayo de 2014
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.

Más respuestas (0)

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by