david
david

Reputation: 49

Removing / Manipulating Data in MATLAB

I have a small problem with some EMG data I have to analyse for a publication I am working on.

I have one column of data with 13000 data points e.g.,

0
0
0
1.2
2.5
3.9
6.8
5.3
2.9
1.5
500
... and so on.

Now the problem I have is that in this data set, there are numerous point, such as the 500 above which crop up in the data. These are not erroneous data, they are just out by a factor of 1000.

My issue is in resolving these data points. I can remove them using the following script

A = (filename)
A(A>100)=NaN

I am unfortunately very new to Matlab, so I was hoping someone could help me use a script that instead of replacing the erroneous number with an NaN, divided it by 1000 to generate a number in the correct units.

Many thanks

David

Upvotes: 2

Views: 95

Answers (2)

kkuilla
kkuilla

Reputation: 2256

A = [0 0 0 1.2 2.5 3.9 6.8 5.3 2.9 1.5 500];

We find all the indices where A>100

(A(A>100))

We take the values at those indices and divide them by 1000, element-wise

A(A>100)./1000

Then assign them back.

A(A>100) = A(A>100)./1000; 

A'
ans =

   0.00000
   0.00000
   0.00000
   1.20000
   2.50000
   3.90000
   6.80000
   5.30000
   2.90000
   1.50000
   0.50000

Upvotes: 2

JoErNanO
JoErNanO

Reputation: 2488

Understanding Logical Indexing

What you are doing with this command

A = (filename)
A(A > 100) = NaN

is called logical indexing. You are basically building a temporary vector of logicals (booleans, 0's and 1's) of size numel(A), setting each element > 100 to 1, and every other element to 0. You then use this temporary logical array to index the vector A. What happens when you do this A(A > 100) is that you are selecting only the items in A corresponding to the items set to 1 in the vector of logicals. For completeness' sake, given

>> A = [0 0 0 1.2 101 3.9 6.8 500];
>> indexes = A > 100
  indexes = [0 0 0 0 1 0 0 1]
>> A(indexes) % This is equal to your A(A > 100)
  ans = [101, 500]

Scaling Down Your Values

Having understood this, what you do next is you are assigning NaN to the selected values: A(A > 100) = NaN. Instead of doing this you could scale them down by whatever factor you need, 1000 in your case:

>> A = [0 0 0 1.2 101 3.9 6.8 500];
>> indexes = A > 100
  indexes = [0 0 0 0 1 0 0 1]
>> A(indexes) % This is equal to your A(A > 100)
  ans = [101, 500]
>> A(indexes) = A(indexes) ./ 1000
  A = [0 0 0 1.2 0.101 3.9 6.8 0.500]

Removing Values

Because you said you are new to Matlab I will add that assigning elements to NaN does not remove them from your vector. What you are doing probably works for you because you are then plotting this data, and Matlab's plot() function does not plot data points with a value of NaN.

The correct way to remove data from a vector is to assign it to an empty value [] like this:

>> A = [0 0 0 1.2 101 3.9 6.8 500];
>> indexes = A > 100
  indexes = [0 0 0 0 1 0 0 1]
>> A(indexes) % This is equal to your A(A > 100)
  ans = [101, 500]
>> A(indexes) = []
  A = [0 0 0 1.2 3.9 6.8]

Upvotes: 1

Related Questions