MATLAB: How to replace values in a matrix

findmatixreplace

Hello,
I am fairly new to MATLAB and I have a question. How do you replace a value in a matrix? So if I have a matrix A, where A=3,2,5,-6. How can I find, and replace all of the negative numbers with 0? So I want to end up with a matrix of 3,2,5,0.
Thank you!
Molly

Best Answer

One way is to use ‘logical indexing’:
A = [3,2,5,-6];
A(A<0) = 0
A =
3 2 5 0