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

1 次查看(过去 30 天)
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

采纳的回答

KSSV
KSSV 2018-7-5
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by