Substitute for join/ outerjoin in cell array?

조회 수: 7 (최근 30일)
phlie
phlie 2016년 9월 6일
답변: Guillaume 2016년 9월 6일
It seems join and outerjoin only work for tables. Is there a substitute for cell arrays?

채택된 답변

Guillaume
Guillaume 2016년 9월 6일
Well, you can always convert your cell arrays to a table with cell2table, do the join and convert back.
Otherwise, it's not particularly hard to implement using ismember:
%INPUTS:
%leftcell: first cell array
%rightcell: second cell array
%leftkeys: column indices for left key
%rightkeys: column indices for right key
%JOIN:
[common, rows] = ismember(leftcell(:, leftkeys), righcell(:, rightkeys), 'rows');
joinedcell = [leftcell(common, :), rightcell(rows, :)];
%OUTERJOIN:
[commonleft, rows] = ismember(leftcell(:, leftkeys), righcell(:, rightkeys), 'rows');
commonright = ismember(rightcell(:, leftkeys), leftcell(:, rightkeys), 'rows');
joinedcell = [leftcell(commonleft, :), rightcell(rows, :)];
joinedcell = [joinedcell; ... normal join
[cell(sum(~commonright), size(leftcell, 2)), rightcell(~commonright, :)]; ... add non matching rows from rightcell
[leftcell(~commonleft, :), cell(sum(~commonleft), size(righcell, 2))]]; ...add non matching rows from leftcell
I've not merged the left and right keys. It's not particularlty hard to do, and you could also select which columns to include in the join, but you should get the idea from the above.

추가 답변 (1개)

KSSV
KSSV 2016년 9월 6일
You can join them...
k{1} = 1 ;
k{2} = [2 3] ;
l{1} = [3,4];
l{2} = [5] ;
% join them
kl = [k l] ;
  댓글 수: 2
phlie
phlie 2016년 9월 6일
Thank you. Am I missing something or is there no merging key in your answer? Join/ outerjoin allows me to join data based on a common value of data k and l, i.e., I can merge even if the data is not ordered the same way (or if the dimensions don't match).
Guillaume
Guillaume 2016년 9월 6일
편집: Guillaume 2016년 9월 6일
@Siva,
The joins ( join, innerjoin, outerjoin) in question are special functions that are not simple concatenation.
The concept comes from relational databases.

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

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by