Match columns containing numbers in two cell arrays.Join the cell arrays.

I have a cell array named data (7886x15 cell). The 15th column of this cell array contains the information of participant number ( 1, 5 , 7, 10 ...and so on). There is another cell array called participant_detail that contains (696x29 cell) in which the 2nd column contains the participant number.
I want to write a command line that matches the participant number (i.e. 15th column of data cell array with the 2nd column of participant_detail cell array). Then picks up the corresponding row of participant_detail cell array and adds it from 16th column onward in the data cell array.
Can anyone help me out.Thanks in advance.

 채택된 답변

Guillaume
Guillaume 2014년 9월 9일
편집: Guillaume 2014년 9월 9일
Assuming that a participant can always be found:
[~, pdrow] = ismember([data{:,15}], [participant_detail{:, 2}]);
data(:,16:44) = participant_detail(pdrow,:);
If not:
[found, pdrow] = ismember([data{:,15}], [participant_detail{:, 2}]);
data(found,16:44) = participant_detail(nonzeros(pdrow),:);

댓글 수: 6

Hi!
This gives the following error Subscripted assignment dimension mismatch.
This must be because participant_detail is not 29 columns as you've stated. Try:
data(:, 16:15+size(participant_detail, 2)) = participant_detail(pdrow, :);
Hi! The ismember command works and I get a (1x12206 double). However running the second line of the code gives the following error : Subscript indices must either be real positive integers or logicals.
avantika
avantika 2014년 9월 10일
편집: avantika 2014년 9월 10일
I am uploading the screen shot of the variable data (7886x15 cell) and pdetail (696x9 cell). I have modified pdetail a bit. The 15th column of the data has to be matched with 1st column of pdetail. The corresponding row of the pdetail needs to be added from 16th column onwards in data.
pdetail
data
Data :
Well, as I said, pdetail is not 29 columns but only 9.
Furthermore, the id column in pdetail is 1, not 2 as you've stated.
You also never mentioned anything about row headers. That completely messes up things.
For this data, the proper code would be:
[~, pdrow] = ismember([data{2:end,15}], [pdetail{2:end, 1}]);
data(2:end,16:24) = pdetail(pdrow + 1,:); %or replace the 24 by 15+size(pdetail, 2) to be safe
If some Pid are missing in pdetail, then:
[found, pdrow] = ismember([data{2:end,15}], [pdetail{2:end, 1}]);
data([0 found],16:24) = pdetail(nonzeros(pdrow)+1,:);
As you can see, having the headers complicates things a bit, so you may be better off removing them.
Note that if the code is working right, ismember should returns as many numbers as the number of rows in data (header not included).
Thanks a lot ! it worked :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

A - cell array 7886x15; B - cell array 696x29
[l,ii] = ismember([A{:,15}],[B{:,2}]);
D = cell(size(A,1),size(B,2));
D(l,:) = B(ii(l),:);
out = [A, D];

댓글 수: 1

this gives the following error: Error using horzcat Dimensions of matrices being concatenated are not consistent.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

질문:

2014년 9월 9일

댓글:

2014년 9월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by