Identifying & replacing the even elements w/i a matrix.

Hello, so I've set up a random matrix as such.
a = 0;
b = 50;
r = (b-a).*rand(5,5) + a
r(r>10) = [0]
This is the code I'm using to identify whether or not certain elements are above 10 & replacing them with a zero(0). I'd like to somehow modify the (r>10) portion to look for whether or not the number is even instead of it being over 10. I've also realized that I'd probably like to have all the elements produced as integers so that this can be done.
I want to produce a random matrix of random integers b/w 1-50 and replace all even elements with a zero, then re-echo the new matrix. I'd like to be able to modify this existing code rather than overhauling it if possible.
note: This is for homework, but we've been encouraged to use this website as our primary help resource.
Thanks.

2 Comments

Since your numbers are double precision numbers, the probability they are even is effectively zero.
So first, learn to use randi to create your matrix. READ THE HELP! The gods of MATLAB put help there for a reason.
Next, how would you test to see if a number is odd or even? What property would it have? Is there a tool that might help you in that quest? After all, this is your homework. We will not do it for you.
Consider the integers:
A = (1:10).';
r = mod(A,2);
[A r]
ans = 10×2
1 1 2 0 3 1 4 0 5 1 6 0 7 1 8 0 9 1 10 0

Sign in to comment.

Answers (0)

Products

Release

R2021b

Asked:

on 15 Feb 2022

Commented:

DGM
on 15 Feb 2022

Community Treasure Hunt

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

Start Hunting!