23 views (last 30 days)

Hi

Please help me how to make a 5x5 matrix whose members are all binary numbers,

For example, select the matrix even elements and then if the binary number is 1111000, convert it to 00001111, in other words, convert one to zero and convert zero to one.

r = 4×4

11001100 11011100 10000010 10010110

10100000 11011000 11010100 10011100

11011001 11001101 10001001 11001011

10001001 11111101 10011100 11111001

convert to

r = 4×4

11001100 00100011 10000010 01101001

10100000 00100111 11010100 01100011

11011001 00110010 10001001 00110100

10001001 00000010 10011100 00000110

Mohammad Sami
on 15 Jan 2021

Edited: Mohammad Sami
on 15 Jan 2021

If your data is int / uint, you can use the function bitcmp to get the bitwise complement.

a = uint8(1);

b = bitcmp(a);

dec2bin(a,8)

dec2bin(b,8)

Walter Roberson
on 15 Jan 2021

bitcmp is a good idea.

If you happen to need to do the negation for some bits but not others, then you can use exclusive or against a pattern that has 1 in the places to flip. You can also always write bitcmp in terms of bitxor, but bitcmp is theoretically faster if you are doing all the bits.

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 6 Comments

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/717450-change-the-binary-number-of-a-specific-group-of-matrix-elements-by-switching-from-zero-to-one-and-vi#comment_1264225

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/717450-change-the-binary-number-of-a-specific-group-of-matrix-elements-by-switching-from-zero-to-one-and-vi#comment_1264225

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/717450-change-the-binary-number-of-a-specific-group-of-matrix-elements-by-switching-from-zero-to-one-and-vi#comment_1264245

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/717450-change-the-binary-number-of-a-specific-group-of-matrix-elements-by-switching-from-zero-to-one-and-vi#comment_1264245

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/717450-change-the-binary-number-of-a-specific-group-of-matrix-elements-by-switching-from-zero-to-one-and-vi#comment_1264260

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/717450-change-the-binary-number-of-a-specific-group-of-matrix-elements-by-switching-from-zero-to-one-and-vi#comment_1264260

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/717450-change-the-binary-number-of-a-specific-group-of-matrix-elements-by-switching-from-zero-to-one-and-vi#comment_1264275

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/717450-change-the-binary-number-of-a-specific-group-of-matrix-elements-by-switching-from-zero-to-one-and-vi#comment_1264275

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/717450-change-the-binary-number-of-a-specific-group-of-matrix-elements-by-switching-from-zero-to-one-and-vi#comment_1264285

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/717450-change-the-binary-number-of-a-specific-group-of-matrix-elements-by-switching-from-zero-to-one-and-vi#comment_1264285

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/717450-change-the-binary-number-of-a-specific-group-of-matrix-elements-by-switching-from-zero-to-one-and-vi#comment_1264370

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/717450-change-the-binary-number-of-a-specific-group-of-matrix-elements-by-switching-from-zero-to-one-and-vi#comment_1264370

Sign in to comment.