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

John D'Errico
John D'Errico 2022년 2월 15일
편집: John D'Errico 2022년 2월 15일
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에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2021b

태그

질문:

2022년 2월 15일

댓글:

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