Join 2 numeric cell arrays using a common key
显示 更早的评论
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!
采纳的回答
更多回答(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)])
类别
在 帮助中心 和 File Exchange 中查找有关 Image Arithmetic 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!