How to match cell arrays according to their sample ID?
    2 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
I have two cell arrays of different size. Now I want to match them into a single cell array according to their Sample ID. 
Here short samples how my initial cell arrays (A & B) look like. 
%% Create sample data
A= {'Sample ID', 'Value'; 1, 1050; 2,1133; 3,1001; 4,1200}
B= {'Sample ID', 'Date'; 1, '01 Dez 2022 14:11:05'; 3,'02 Dez 2022 15:10:10'; 4,'02 Dez 2022 15:11:45'}
A:
Sample ID    Value    
1                    1050
2                    1133
3                    1001
4                    1200
B:
Sample ID    Date
1                   '01 Dez 2022 14:11:05'
3                   '02 Dez 2022 15:10:10'
4                   '02 Dez 2022 15:11:45'
They should be combined to one cell array so that sample IDs match. For samples that only exist in array A or array B the missing value should be filled with NaN. The output should look as follows:
C:
Sample ID    Date                                    Value
1                    '01 Dez 2022 14:11:05'       1050
2                    NaN                                    1133
3                    '02 Dez 2022 15:10:10'       1001
4                    '02 Dez 2022 15:11:45'       1200
I tried it with ismember, intersect and a loop however encoutered basic problems when using the functions:
%% Tried approaches
% intersect
[val,pos]=intersect(A{:,1},B{:,1});
% --> error with too many input arguments
% ismember
common=ismember(A{:,1},B{:,1});
% --> error with too many input arguments
% loop
C(:,[1 3])=A(:,1:2);
i=2;
k=2;
for i=2:length(A)
    if B{k,1}==A{i,1}
        C{i,2}==B{k,2};
        i=i+1;
        k=k+1;
    else
        C{i,2}=nan;
        i=i+1;
    end
end
% --> problems with incompatible sizes 
How can I do this? Thanks in advanced for any advice!
2 Commenti
  Jan
      
      
 il 3 Dic 2022
				
      Modificato: Jan
      
      
 il 3 Dic 2022
  
			It would be useful, if you post the input data such, that they can be used by copy&paste to post an answer. It is tedious to construct such data, so it should be your turn.
Then I would not have to guess, if "Sample ID    Date" belong to the data or not.
"I tried it with ismember and intersect but did not work as desired." - It is useful to post the code and to explain the occurring problems. Sometime a small modification can fix the code.
Risposta accettata
  Star Strider
      
      
 il 3 Dic 2022
        Try something like this — 
%% Create sample data
A= {'Sample ID', 'Value'; 1, 1050; 2,1133; 3,1001; 4,1200};
B= {'Sample ID', 'Date'; 1, '01 Dez 2022 14:11:05'; 3,'02 Dez 2022 15:10:10'; 4,'02 Dez 2022 15:11:45'};
At = cell2table(A(2:end,:), 'VariableNames',A(1,:))
Bt = cell2table(B(2:end,:), 'VariableNames',B(1,:));
Bt.Date = datetime(Bt.Date,'Locale','de_DE', 'InputFormat','dd MMM yyyy HH:mm:ss')
C = outerjoin(At,Bt, 'Keys',{'Sample ID'})
CVN = C.Properties.VariableNames;
C = removevars(C,CVN(3))
This can be done relatively easily using cell2table to create a table out of each cell array, then outerjoin and removevars to get the desired result.  The ‘NaT’ (‘Not a Time’) entry is ‘NaN’ for datetime variables.  Use table2cell to convert the final result back into a cell array, if necessary.  
.
2 Commenti
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su Data Type Conversion in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


