How to rule out (filter for a continuous function) some output data?

Hello,
t1=[1:1:100]
p=xlsread('Jan2010.xls','B101:B200');
t2=[101:1:200]';
k(t2)=a1*sin(b1*t2+c1)+a2*sin(b2*t2+c2);
if k(t2) > max(p) || k(t2) < min(p);
k(t2)=k(t2-t1);
end
v=k(t2)';
error:
Operands to the || and && operators must be convertible to logical scalar values
when I change to |, it doesn't show any error, but it doesn't have any effect on the function too.
How can I get rid of this error?
Anyway, I used another code (below); I didn't receive any error, however I didn't see any change in the result either!
if k(t2)>max(p)
k(t2)=k(t2-t1);
elseif k(t2)<min(p)
k(t2)=k(t2-t1);
end
I need this to rule out(filter) input data that is not relevant (because they are more than or less than 'max' or 'min' of p). so that when I draw the function k(t2) the final values of k doesn't exceed max and min of p.
How can I get rid of some particular results that a functions shows(the function is a sinusoidal diagram in this case)?

2 commentaires

Do you realize that you said you have two questions and yet didn't ask any questions?
Peyman
Peyman le 9 Août 2012
Modifié(e) : Peyman le 9 Août 2012
Corrected it. means we are going somewhere, though no result yet

Connectez-vous pour commenter.

 Réponse acceptée

Maybe try it like this:
if k(t2) > max(p) || k(t2) < min(p) % Semicolon not needed.
k(t2) = k(t2 - 1); % Set to previous element.
end

12 commentaires

@Image Analyst: Nope, didn't work, same error. (How did you write like that?! I'm also trying the help to write clearly, but it doesn't work for me)!! like next line, ....
Image Analyst
Image Analyst le 9 Août 2012
Modifié(e) : Image Analyst le 9 Août 2012
Highlight the text you want to be formatted as code, and click the "{} Code" button above your edit box. Please give an example with actual numbers for k, t2, t1, and p so we can examine your actual code with your actual numbers, or at least demo numbers that illustrate the problem.
if k(t2) > max(p(:)) || k(t2) < min(p(:)) % Semicolon not needed.
k(t2) = k(t2 - 1); % Set to previous element.
end
Ok;Thanks; This one also didn't work.
Please expand on "didn't work". Is it producing an error message?
Yes, same error
It's because your t2 is a vector, so k(t2) is a vector. What do you want to happen if the condition is true for some members of k but not for others?
After seeing your code, it looks like p is a 2D array, so min(p) is a 1D array which is the min per column over all the rows (the min vertically). I think you want max(p(:)) and min(p(:)).
Also, since t1 is an array, k(t2) will also be an array and so k(t2)>max(p(:)) will be an array. I think you need to get indexes like
badIndexes = k(t2) > max(p(:)) | k(t2) < min(p(:))
% Set bad indexes = prior indexes.
% Need to modify to handle cases where there are
% two or more bad ones in a row!
k(badIndexes) = k(badIndexes-1);
Peyman
Peyman le 9 Août 2012
Modifié(e) : Peyman le 9 Août 2012
@Walter Roberson:
I need this to rule out(filter) input data that is not relevant (because they are more than or less than 'max' or 'min' of p). so that when I draw the function k(t2) the final values of k doesn't exceed max and min of p.
@Image Analyst:
I don't know anything about what you said; Anyway, I just substituted what you gave me with that part of the code, and shows up this error:
Subscript indices must either be real positive integers or logicals.
Error in .... (line ...)
k(badIndexes) = k(badIndexes-1);
It's time you learned how to use the debugger to figure this out for yourself. Set up temporary variables that you can inspect to see if they are scalars or arrays. Are you using the debugger? If not, why not? It's much faster than debugging via the Answers forum postings. It's also thoughtful for people who want to help you to provide sample data so they can run your program. I had to do that myself. Here's what I have:
clc;
t1=[1:1:100]
% p=xlsread('Jan2010.xls','B101:B200');
p=rand(1,100);
t2=[101:1:200]';
a1 = 2;
a2 = 3;
b1 = 2;
b2 = 4;
c1 = 0;
c2 = 0;
k(t2)=a1*sin(b1*t2+c1)+a2*sin(b2*t2+c2);
minPvalue = min(p(:))
maxPvalue = max(p(:))
% Now take the prior good index.
% TODO: Handles cases where the first element(s) are bad.
lastGoodValue = k(1);
% Sca the array, setting bad values to the last good value.
for kIndex = 2 : length(k)
itsABadIndex = k(kIndex) > maxPvalue || k(kIndex) < minPvalue;
if itsABadIndex
% It's bad, so fix it.
k(kIndex) = lastGoodValue;
end
lastGoodValue = k(kIndex);
end
k
I've left it to you to figure out what to do if the k array start out with bad values.
Sorry about that and thank you so much.
What debugger do you suggest.
I didn't follow at the end, what did you leave to me?! You already did it!
MATLAB has a built in debugger. See some of Doug Hull's video tutorials on the web site - he has a blog. I did not handle the case where k(1) is a bad value but you see how to do it from the inside of the for loop.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Functions dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by