arrays - Reordering a vector in Matlab? -
i have vector in matlab b
of dimension nx1
contains integers 1
n
in order, e.g. n=6 b=(2;4;5;1;6;3)
.
i have vector a
of dimension mx1
m>1
contains same integers in ascending order each 1 repeated arbitrary number of times, e.g. m=13
a=(1;1;1;2;3;3;3;4;5;5;5;5;6)
.
i want c
of dimension mx1
in integers in a
reordered following order in b
. in example, c=(2;4;5;5;5;5;1;1;1;6;3;3;3)
one approach ismember
, sort
-
[~,idx] = ismember(a,b) [~,sorted_idx] = sort(idx) c = b(idx(sorted_idx))
if one-liners, bsxfun
-
c = b(nonzeros(bsxfun(@times,bsxfun(@eq,a,b.'),1:numel(b))))