replacing values in a vector with another value in matlab

To replace values in a vector with another value in Matlab, you can use indexing.

For example, let's say you have a vector x that contains the following values:

main.m
x = [1 2 3 4 5];
17 chars
2 lines

If you want to replace all occurrences of the value 2 with the value 0, you can do the following:

main.m
x(x == 2) = 0;
15 chars
2 lines

This code will find all indices in the vector where the value is equal to 2, and replace those values with 0.

Similarly, you can use the same indexing technique to replace any value in the vector with another value.

gistlibby LogSnag