Join 2 numeric cell arrays using a common key
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Max Leh
el 3 de Nov. de 2016
Respondida: Max Leh
el 3 de Nov. de 2016
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 comentarios
Respuesta aceptada
Jan
el 3 de Nov. de 2016
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 comentarios
Más respuestas (2)
KSSV
el 3 de Nov. de 2016
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 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!