Matlab: creating a bigger array using a smaller array -
i have 2 giant array looks like:
a = [11, 11, 12, 3, 3, 4, 4, 4 ]; b = [ 12, 4; 3, 11; 11, 1; 4, 13 ];
i want create array takes values b , column 1 like:
c = [ 11, 1; 11, 1; 12, 4; 3, 11; 3, 11; 4, 13; 4, 13; 4, 13 ];
i don't want use or other kind of loop optimize process.
sorry being terse.
i search each element column 1 of in column 1 of b , pick corresponding column 2 elements b , create new array column 1 elements of , discovered column 2 elements b.
what doing in problem using a
, searching first column of b
see if there's match. once there's match, extract out row corresponds matched location in b
. repeat rest of values in a
.
assuming values of a
can found in b
, first column of b
distinct , there no duplicates, can unique
call , sortrows
call. unique
call on a
can assign each value in a
unique label in sorted order. use these labels index sorted version of b
desired result:
[~,~,id] = unique(a); bs = sortrows(b); c = bs(id,:);
we c
:
c = 11 1 11 1 12 4 3 11 3 11 4 13 4 13 4 13
Comments
Post a Comment