Creating a new matrix from an existing one using logical operators?

10 Ansichten (letzte 30 Tage)
Trisha Katz
Trisha Katz am 20 Okt. 2019
Beantwortet: David Hill am 20 Okt. 2019
Write a script which defines a 100x100 matrix, X, of random integers between 5
and 50. From the matrix X, create a matrix, Y, in which all values greater than or
equal to 25 are the same as in X, but all values below 25 are changed to 0.
Here is what I have:
x=randi([5 50],100,100)
Y=randi([5 50],100,100)
if Y>25
disp(Y)
elseif Y<25
disp(0)
end
The matrix that I am getting in the out put is not correct. No numbers have been substituted with zero.
  2 Kommentare
the cyclist
the cyclist am 20 Okt. 2019
If you look at the documentation on the if function, you'll see that in the "Compare Arrays" section, it says
"Expressions that include relational operators on arrays, such as A > 0, are true only when every element in the result is nonzero."
So, your if statement is not testing each element and then giving a result for each element, as you seem to expect. It is testing the entire array, to see if it is true for all elements.
Trisha Katz
Trisha Katz am 20 Okt. 2019
Okay, I understand, but how can I get it search for specific values within the matrix? i have tried googling and looking through my text to no avail

Melden Sie sich an, um zu kommentieren.

Antworten (1)

David Hill
David Hill am 20 Okt. 2019
x=randi([5 50],100,100);
y=(x>=25).*x;

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by