ARCHIVED: In MATLAB, how can I find the small elements of a vector, and how do I change their values?

This content has been archived, and is no longer maintained by Indiana University. Information here may no longer be accurate, and links may no longer be available or reliable.

In MATLAB, you can use the find function to find the elements of a vector satisfying some condition. For example, the following command will find all elements of a vector called x that are less than .5, and store their indices in a vector called i:

  i = find(x<.5);

Suppose, for example, you wish to replace all of the elements of vector x that are less than .5 with 0. You can use the above command, and then the following:

  x(i) = 0;

You can combine these into one command:

  x(find(x<.5)) = 0;

To set all but the ten largest elements of a vector x to zero, use the following command:

  [y,j] = sort(x);
  x( j(1:(size(x,1) - 10)) ) = 0*j(1:(size(x,1) - 10));

If you have questions about using statistical and mathematical software at Indiana University, contact the UITS Research Applications and Deep Learning team.

Related documents

This is document aclw in the Knowledge Base.
Last modified on 2023-05-09 14:39:15.