I have matrix like below;

x1(1,:)=[0.1,0.9,0.9,0.9,0.1, 0.9,0.1,0.9,0.1,0.9, 0.9,0.9,0.1,0.9,0.9, 0.9,0.1,0.9,0.1,0.9, 0.1,0.9,0.9,0.9,0.1];

and I want to corrupt that pattern with a given specific percentage. For instance, if I want to corrupt pattern %8 then 2 elements of matrix has to be changed (if the element is 0.9, it should be 0.1 and vice versa).

For matrix bleow;

x(:,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,-1,1]';

I have tried this function and it works fine.

function Ret=corrupt(Inx,per)

    I_SIZE=length(Inx);
    N_COR=floor(I_SIZE*per/100);

    I_INX=randperm(I_SIZE);
    C_INX=I_INX(1:N_COR);
    Inx(C_INX)= -1 * Inx(C_INX);

    Ret=Inx;

How can I make changes for 0.1 and 0.9?

有帮助吗?

解决方案

It may be easier using randsample:

C_INX = randsample(1:I_SIZE, N_COR);
Inx(C_INX) = 1-Inx(C_INX);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top