Identifying & replacing the even elements w/i a matrix.
Ältere Kommentare anzeigen
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 Kommentare
John D'Errico
am 15 Feb. 2022
Bearbeitet: John D'Errico
am 15 Feb. 2022
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]
Antworten (0)
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!