How to compare 5 cell arrays and get their contents in one cell array?

1 view (last 30 days)
Hello gyus,
I have a cell array of size 600*1 and 4 cell arrays: cell1(361*2), cell2(376*2), cell3(362*2) and cell4(351*2).
cell array(600*1) have overlap rows with all of these 4 cell arrays column1 rows;
I want to output a cell of size 600*4 with the 600 rows and 4 columns, which are the contents of the 4 cells column 2 contents if there is an overlap with their rows , if not put 0.
More specifically some rows in cell array( 600*1) may exist in all the rows of all the 4 cells or may exist in just some of them.
Inputs: cell array, cell1, cell2, cell3, and cell4
I appreciate any help plz!
Original cell:
'R-HSA-3000171:R-HSA-3000171'
'R-HSA-3000178:R-HSA-3000178'
'R-HSA-3000471:R-HSA-3000471'
'R-HSA-3000480:R-HSA-3000480'
'R-HSA-3000484:R-HSA-3000484'
cell1
'R-HSA-3000171:R-HSA-3000171' 'PDGFB'
'R-HSA-5419276:R-HSA-5419276' ' MRPS30,MRPL48,MRPS33'
cell2
'R-HSA-3000171:R-HSA-3000171' 'TTR'
'R-HSA-5655302:R-HSA-5655302' 'FGF1,FGF17'
cell3
'R-HSA-3000171:R-HSA-3000171' 'COL4A6,LAMA1,COL4A5'
'R-HSA-416482:R-HSA-416482' 'GNB2,ARHGEF16,ABR'
cell4
'R-HSA-3000171:R-HSA-3000171' 'COL11A2,COL3A1'
'R-HSA-5419276:R-HSA-5419276' 'MRPL1'
Iwanted_output:
Iwanted_output.png
  3 Comments
chocho
chocho on 14 Dec 2018
@KSSV Thanks for your suggestions, My problem not in which command i can use but after alocate a cell of 600 rows and 5 cells , how i can place those columns altogether. foe evry row?

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 14 Dec 2018
Edited: Stephen23 on 14 Dec 2018
Use ismember in a loop:
C0 = {'A';'B';'C';'D';'F'}; % your 600x1 cell array.
C1 = {'A',1;'C',123;'X',0};
C2 = {'B',21;'D',2222};
C3 = {'C',321;'F',333333;'A',111111};
C4 = {'C',444};
CN = {C1,C2,C3,C4}; % all inputs in one cell array.
CZ = cell(numel(C0),numel(CN)); % preallocate output array.
for k = 1:numel(CN)
[idx,id0] = ismember(CN{k}(:,1),C0);
CZ(id0(idx),k) = CN{k}(idx,2);
end
  7 Comments
Jan
Jan on 14 Dec 2018
Edited: Jan on 14 Dec 2018
@chocho: Your question was: "I want to output a cell of size 600*4 with the 600 rows and 4 columns". This sounds clearly as 4 columns, not 5.
So you want to add C0 as first column? Easy:
CZ = cat(2, C0, CZ)
Or modify the code:
CZ = cell(numel(C0), numel(CN) + 1); % preallocate output array.
CZ(:) = {0}; % Or {'0'} or {'[]'} or whatever you want
CZ(:,1) = C0;
for k = 1:numel(CN)
[idx, id0] = ismember(CN{k}(:,1), C0);
CZ(id0(idx), k + 1) = CN{k}(idx, 2);
end
Stephen23
Stephen23 on 14 Dec 2018
"i was thinking that you will do these 2 things inside your code inside the for loop "
Yes, you could also do that, that is a good idea. It might even be more efficient and/or simpler to do those steps inside the loop.... you should give it a try.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!