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 个评论

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

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品

版本

R2021b

标签

评论:

DGM
2022-2-15

Community Treasure Hunt

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

Start Hunting!

Translated by