Monday, January 14, 2008

Replacing certain values in matlab vectors

In Matlab if you need to replace certain values with a single value you can simple do this,

>u(find(isnan(u))) = [];

Using this will find and remove all the NaN (Not a Number) values.

If you want to remove any value you can simple use find again,

>u(find(u=9999)) = [];


This will remove all the values of u = 9999.

Be aware that applying this will make your vector shorter if there is any value removed.

1 comment:

Anonymous said...

You can actually skip the find command. This is logical indexing, which usually works faster.