The number of swap should be equal to number of one in vector z divide by 2 if even ones else plus one divide by 2.The value of z change after each iteration so logic should be universal.
MATLAB: Suppose a row vector x=1:8 and another vector of same size z=[1 1 0 1 1 0 1 1].I want to swap element in x according to element in z if z==1 then swap the postion of element of x else z==0 element of x should be in same position in vector.
also make sure resultant vector x contain all unique element.
Related Question
- Permutation of only x elements of a vector
- How to convert a 64 bit, two’s complement value(binary vector)[ 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1] into a signed integer
- How to avoid repeat numbers in a row
- Randomly select different elements of a vector
- Randperm without number 1
- -1 and 1 matrix
Best Answer