How to replace non-zero elements randomly with zero?

Saba

I have a matrix including 1 and 0 elements like below which is used as a network adjacency matrix.

A =

    0     1     1     1
    1     1     0     1
    1     1     0     1
    1     1     1     0

I want to simulate an attack on the network, so I must replace some specific percent of 1 elements randomly with 0. How can I do this in MATLAB?

I know how to replace a percentage of elements randomly with zeros, but I must be sure that the element that is replaced randomly, is one of the 1 elements of matrix not zeros.

Luis Mendo

If you want to change each 1 with a certain probability:

p = 0.1%; % desired probability of change

A_ones = find(A); % linear index of ones in A
A_ones_change = A_ones(rand(size(A_ones))<=p); % entries to be changed
A(A_ones_change) = 0; % apply changes in those entries

If you want to randomly change a fixed fraction of the 1 entries:

f = 0.1; % desired fraction

A_ones = find(A);
n = round(f*length(A_ones));
A_ones_change = randsample(A_ones,n);
A(A_ones_change) = 0;

Note that in this case the resulting fraction may be different to that intended, because of the need to round to an integer number of entries.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Perform numpy product over non-zero elements of a row

분류에서Dev

mean of non zero elements - why are these two attempt returning different results?

분류에서Dev

How to zero a dynamic array?

분류에서Dev

Rectangle custom object shows zero width, should be non-zero

분류에서Dev

mysql group by and average for non zero values

분류에서Dev

SQL select records with non zero parameter

분류에서Dev

How to compare signal to zero in vhdl?

분류에서Dev

How to zero a disk + verify it afterward?

분류에서Dev

C# regex split for A non-zero digit followed by an arbitrary number of zero

분류에서Dev

solving tridiagonal system with non-zero opposite corners in python efficiently

분류에서Dev

mail: cannot send message: process exited with a non-zero status

분류에서Dev

iOS non-zero reference count in ARC and cannot dealloc

분류에서Dev

Non causal zero-phase FIR with the windowing method

분류에서Dev

Non causal zero-phase FIR with the windowing method

분류에서Dev

Windows command line - filter lines with non-zero values

분류에서Dev

How does ruby handle zero division?

분류에서Dev

how to keep zero at last without using sort

분류에서Dev

How to catch a divide by zero error in Haskell?

분류에서Dev

How to ensure a unique number field with zero order

분류에서Dev

how to store zero leading numbers in mongoDB

분류에서Dev

how to see if associative array key is equal to zero?

분류에서Dev

How to create a new LocalDateTime containing ZERO timezone?

분류에서Dev

How to print out a string with zero-padding?

분류에서Dev

How to keep the date in DateTimeOffset and reset the offset to zero (UTC)?

분류에서Dev

How To Make Justify Content flex-end, Zero Bottom on Flutter

분류에서Dev

How can I zero-pad numbers in a Batch FOR loop?

분류에서Dev

How to add Zero before the name depending on name lenght

분류에서Dev

R: How to check if a matrix is different of a matrix with zero's

분류에서Dev

Python 3.4: how to add zero milliseconds and change their formatting

Related 관련 기사

  1. 1

    Perform numpy product over non-zero elements of a row

  2. 2

    mean of non zero elements - why are these two attempt returning different results?

  3. 3

    How to zero a dynamic array?

  4. 4

    Rectangle custom object shows zero width, should be non-zero

  5. 5

    mysql group by and average for non zero values

  6. 6

    SQL select records with non zero parameter

  7. 7

    How to compare signal to zero in vhdl?

  8. 8

    How to zero a disk + verify it afterward?

  9. 9

    C# regex split for A non-zero digit followed by an arbitrary number of zero

  10. 10

    solving tridiagonal system with non-zero opposite corners in python efficiently

  11. 11

    mail: cannot send message: process exited with a non-zero status

  12. 12

    iOS non-zero reference count in ARC and cannot dealloc

  13. 13

    Non causal zero-phase FIR with the windowing method

  14. 14

    Non causal zero-phase FIR with the windowing method

  15. 15

    Windows command line - filter lines with non-zero values

  16. 16

    How does ruby handle zero division?

  17. 17

    how to keep zero at last without using sort

  18. 18

    How to catch a divide by zero error in Haskell?

  19. 19

    How to ensure a unique number field with zero order

  20. 20

    how to store zero leading numbers in mongoDB

  21. 21

    how to see if associative array key is equal to zero?

  22. 22

    How to create a new LocalDateTime containing ZERO timezone?

  23. 23

    How to print out a string with zero-padding?

  24. 24

    How to keep the date in DateTimeOffset and reset the offset to zero (UTC)?

  25. 25

    How To Make Justify Content flex-end, Zero Bottom on Flutter

  26. 26

    How can I zero-pad numbers in a Batch FOR loop?

  27. 27

    How to add Zero before the name depending on name lenght

  28. 28

    R: How to check if a matrix is different of a matrix with zero's

  29. 29

    Python 3.4: how to add zero milliseconds and change their formatting

뜨겁다태그

보관