문제

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