how to throw out certain intervals.
4 views (last 30 days)
Show older comments
hi all,
this question may be easy for some but i dont even have a clue to how to do it.
here is how i want to do:
I have a vector that is used for intervals, say X = [0 5 6 9];
the code does calculation using values in this way: uses: 0 5, 5 6, 6, 9.
I want to check what the y values are in between each of those intervals, and if a certain condition is not met, i want to skip them. obviously i will be using an if statement, but how do i tell matlab to check the intervals? i really had been trying!!
n = numel(X); for i = 1:n
if min(X(1:1:2)) < * skip
0 Comments
Answers (5)
Wayne King
on 23 Aug 2012
Edited: Wayne King
on 23 Aug 2012
X = [0 5 6 9];
y = 5+randn(10,1);
for nn = 1:numel(y)
if (y(nn)> X(1) & y(nn)<X(2))
disp('y is in interval 1');
else
disp('y is not in interval 1');
end
end
You can modify according to check for other intervals
Babak
on 23 Aug 2012
Like this:
for j=1:length(X)-1
if y>X(j) && y<X(j+1)
% do ...
end
end
2 Comments
Babak
on 23 Aug 2012
That's what the if loop does for you. If the if condition is ture it does whatever is after the if condition but if the if condition is not true MATLAB simply skips whatever is after if up to where you say end or you say else.
Wayne King
on 24 Aug 2012
I'm confused, your original post stated:
I have a vector that is used for intervals, say X = [0 5 6 9];
_I want to check what the y values are in between each of those intervals, and if a certain condition is not met, i want to skip them. obviously i will be using an if statement, but how do i tell matlab to check the intervals? i really had been trying!!
_
From that I understood that you had two vectors, an X vector of interval bounds and a Y vector and you wanted to check each Y value against those intervals.
Is that what you're doing?
What do you think this is doing for you?
Y = X();
What are you trying to do with this loop?
X = [ 2 3 4 57 36 2 ];
for j = 1:numel(X)
if X(j) > 10
Y = X();
end
end
Matt Fig
on 24 Aug 2012
Is this what you want?
X = [ 2 3 4 57 36 2 ];
Y = X(X>10)
I take it this is what you are after because of a loop you posted in one of the questions above. But what has this to do with intervals? If you want good help, you have to slow down, take a breath and explain all of the problem you are having and include all of the variables (or simplified versions) you need to deal with to solve the problem.
Preferably you will give example INPUTS and EXPECTED OUTPUTS for those inputs.
So, I take it that the inputs are:
x1 = [0 2 3 4 5];
x2 = [ 2 3 4 5 6];
But then you mention a set of y values. Show the y-values or a subset of them if there are too many. Then show what you want as the output of the code.
5 Comments
Matt Fig
on 24 Aug 2012
Hold on. In the loop, Y ( note the capital! ) is the set of all values of y ( note the lower case!) that fall between x(ii) and x(ii+1). Now of course there will be a maximum value in any set of numbers, even if there is only one number in the set. What do you mean you want to throw it out if there is a maximum? Throw out everything in Y if Y has a maximum? Or just throw out the maximum value in each Y before going on to the conditional?
Again, give an example. I asked you for inputs and outputs. You gave two inputs ( x and y ) and two outputs ( x3 and x4 ). Now you are saying there is another input? Why did you not show it the first time?
Kelly Kearney
on 24 Aug 2012
Edited: Kelly Kearney
on 26 Aug 2012
I'm not exactly sure of what you want (in your example, your x data seems to match your intervals exactly, which is a little confusing). But in the example below, histc and accumarray can let you quickly organize data into bins, and then perform a function on that data based on bin number.
Note: I've updated this answer to correspond to the example you provided for Matt Fig, assuming that your Y values are indexed as 1:length(y). However, my method falls apart now, since you are using bin definitions that exclude the lower limit but include the upper one, while histc makes the opposite assumption (i.e. histc says that the second value of y falls into bin 2, 2<=x<4, while you say it falls in bin 1, 0<x<=2). The accumarray part of my code might still be useful.
y = [ 0.66 0.04 0.93 0.68 0.76 0.74 0.39 0.66 0.85 0.17]';
x = (1:length(y))';
xint = [0 2 4 6 8 10];
[n, bin] = histc(x, xint);
ystar = 0.9;
fun = @(a) max(a)>ystar;
bunq = unique(bin);
isgood = false(size(xint));
isgood(bunq) = accumarray(bin, y, [], fun);
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!