Join 2 numeric cell arrays using a common key
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi all, I need to join 2 cell arrays. One is shorter than the other. The resulting cell array should (or matrix...anything I can plot) have the length of the shorter array. They have conceptually this structure:
DATA_A =
[1] [1111]
[2] [2222]
[3] [3333]
[4] [4444]
[5] [5555]
DATA_B =
[1] [2.1]
[4] [2.2]
[5] [2.3]
what I want:
DATA_C =
[1111] [2.1]
[4444] [2.2]
[5555] [2.2]
so the first column should be used as a key to combine both arrays. Array A contains all integers, while Array B has "gaps". hope its clear and thanks a lot!
댓글 수: 0
채택된 답변
Jan
2016년 11월 3일
DATA_A = {[1], [1111]; ...
[2], [2222]; ...
[3], [3333]; ...
[4], [4444]; ...
[5], [5555]};
DATA_B = {[1], [2.1]; ...
[4], [2.2]; ...
[5], [2.3]};
keyA = cat(2, DATA_A{:, 1});
keyB = cat(2, DATA_B{:, 1});
[iB, iA] = ismember(keyB, keyA);
DATA_C = cat(2, DATA_A(iA, 2), DATA_B(iB, 2));
I cannot guess what "anything I can plot" exactly mean. Perhaps you want a cell2mat or:
DATA_C = [cat(1, DATA_A{iA, 2}), cat(1, DATA_B{iB, 2})];
댓글 수: 0
추가 답변 (2개)
KSSV
2016년 11월 3일
clc; clear all ;
A ={[1] [1111]
[2] [2222]
[3] [3333]
[4] [4444]
[5] [5555]} ;
B ={[1] [2.1]
[4] [2.2]
[5] [2.3]} ;
% change cells into matrix
A = cell2mat(A) ;
B = cell2mat(B) ;
%
[val,idx] = ismember(B(:,1),A(:,1),'legacy') ;
iwant = num2cell([A(idx,2) B(:,2)])
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!