Index Match - distribute a set of data to a larger set

1 view (last 30 days)
Hi I have the following 2 arrays A = [9 9 9 10 10 10 11 12 12 12 12 13 14 14 14 15 15 16 16 16 16 17]' B = [10 13 16; 999 900 950]' I am looking for a result set C = [9 9 9 10 10 10 11 12 12 12 12 13 14 14 14 15 15 16 16 16 16 17;0 0 0 999 999 999 0 0 0 0 0 900 0 0 0 0 0 950 950 950 950 0]' I have achieved it using loop and repelem, but i am sure there's got to be a simpler solution. Thanks

Accepted Answer

KSSV
KSSV on 5 Jul 2018
A = [9 9 9 10 10 10 11 12 12 12 12 13 14 14 14 15 15 16 16 16 16 17]' ;
B = [10 13 16; 999 900 950]' ;
C = [9 9 9 10 10 10 11 12 12 12 12 13 14 14 14 15 15 16 16 16 16 17;0 0 0 999 999 999 0 0 0 0 0 900 0 0 0 0 0 950 950 950 950 0]' ;
iwant = zeros(length(A),2) ;
iwant(:,1) = A ;
[c,ia] = ismember(A,B(:,1)) ;
for i = 1:max(ia)
iwant(ia==i,2) = B(i,2) ;
end

More Answers (0)

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!