Error with find command for a matrix

20 views (last 30 days)
Jason Fender
Jason Fender on 5 Mar 2011
Hello,
I have data points from a DAQ with a time and signal. My goal is to find the period of my data points. To do this, I wanted to find the maximum peak value and the minimum peak value. If I have these, I should be able to find the corresponding time to those values, which would be half my period since the shape of my data is basically a repeating sine wave.
Here is what I have so far:
_____________________________________________________
clc;
format short
%Compose matrix with time vecotr and signal vector
Test1Signal1=[Test1_Time, Test1_Signal1];
%Find the maximum value
a=max(Test1Signal1,[],1);
a=a(:,2)
%Find the mimumum value
b=min(Test1Signal1,[],1);
b=b(:,2)
%Find the time that corresponds to the maximum value (a)
[r c]=find(Test1Signal1,a)
%Find the time that corresponds to the minimum value (b)
[r c]=find(Test1Signal1,b)
_____________________________________________________
The problem is, I keep getting an error. The error I'm getting is as follows:
??? Error using ==> find Second argument must be a positive scalar integer.
Error in ==> Test1 at 16 [r c]=find(Test1Signal1,a)
I cannot figure out why the code is giving me this error. The value of a (the maximum) is a positive number. Again, I want this to give me the row and column that corresponds to the maximum value so that I can find the time from the original matrix that matches this value.
If that can get figured out, I might have another problem. The minimum value is a negative number. I'm not sure if the find command is going to work on a negative number, but that might be another problem once I get there. Any help would be greatly appreciated.
Thanks

Answers (2)

Paulo Silva
Paulo Silva on 5 Mar 2011
[r c]=find(Test1Signal1==a)
instead of
[r c]=find(Test1Signal1,a)

Walter Roberson
Walter Roberson on 5 Mar 2011
find() does not take two arguments that are an array to search in and the value to search for. The first argument to find() is interpreted as a logical condition with all non-zero values corresponding to "true". Usually find() then returns the locations of all of the positions that are considered true, but find() takes an optional numeric argument that must be a positive integer that tells it how many indices to return. Thus, find(Test1Signal1,a) is requesting that find() return the first "a" locations of Test1Signal that are non-zero. For example, find(Test1Signal1,2) would return the indices of two non-zeros.
The code you are looking for is
[r c] = find(Test1Signal1==a)
I suggest, though, that you consider
[a, minloc] = min(Test1_Signal1)
[b, maxloc] = max(Test1_Signal1)
and then the times would be Test1_Time(minloc) and Test1_Time(maxloc)
Your logic is fragile, though: due to noise or due to the sampling rate not being exactly a multiple of your frequencies, the minimum over the entire signal is not necessarily in the same period as the maximum value.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!