Get new variable with elements from a double matrix & cell array given a specific condition
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponse acceptée
Sven
le 27 Mai 2014
Modifié(e) : Sven
le 27 Mai 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.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Data Type Identification dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!