how to throw out certain intervals.

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

Answers (5)

Wayne King
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

1 Comment

it seems i have a poor if statement because it doesnt seem to work.
X = [ 2 3 4 57 36 2 ]; for j = 1:numel(X) if X(j) > 10 Y = X(); end end Y should be of size 2 but it includes all of the values?

Sign in to comment.

Like this:
for j=1:length(X)-1
if y>X(j) && y<X(j+1)
% do ...
end
end

2 Comments

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.
it seems i have a poor if statement because it doesnt seem to work.
X = [ 2 3 4 57 36 2 ];
for j = 1:numel(X)
if X(j) > 10
Y = X();
end
end
Y should be of size 2 but it includes all of the values?

Sign in to comment.

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

1 Comment

hello
x1 = [0 2 3 4 5];
x2 = [ 2 3 4 5 6];
i do computation using following intervals:
0 2, 2 3, 3 4
i take nth element in x1 and x2. but here is the thing. there are y values associated with each x values. lets suppose that i want to only do a computation if the maxiumum y value in an interval meets a condition. thats what im trying to do...does it make sense? i can try an explain better let me know! i dont know how to do this!
with that loop i want to create a new vector Y that holds the values of X that are grater than 10.

Sign in to comment.

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

Matlabbey
Matlabbey on 24 Aug 2012
Edited: Matlabbey on 24 Aug 2012
thank you for your comments. please forgive my lack of communication. here is an example
x = [0 2 4 6 8 10];
y = [0.89 0.77 0.49 0.75 0.66 0.97 0.11 0.83 0.56 0.12];
x1 = [0 2 4 6 8];
x2 = [ 2 4 6 8 10];
the numbers here are just example. really, the x values are spread more an there are more y values in between.
lets say
the first interval will be 0-2, for the y values we have 0.89 and 0.77. if these two y values do not meet a certain condition than i want to throw out 0 and 2 from x1 and x2. i want two new vectors x3 and x4 that contain the x1 and x2 values for wich the condition on y was met. i will try to explain better if this isnt making sense. thank you!
O.k., so here is what I think you are getting at. We don't actually need x1 and x2 for the code. See if this works for you:
x = [0 2 4 6 8 10];
y = rand(1,20)*10 % Some random numbers on [0 10]
x3 = [];
x4 = [];
for ii = 1:length(x)-1
Y = y(y>x(ii) & y<x(ii+1)); % Has the y vals you need.
conditional = round(rand)
if conditional % This is the conditional....
x3 = [x3,x(ii)];
x4 = [x4,x(ii+1)];
end
end
thanks you!!!!
sorry but i have one more to try to do that i havent figure out this whole time.
Y = y(y>x(ii) & y<x(ii+1)); % Has the y vals you need.
i want to say: if there is a maximum value of y in this interval. basically to say if the maximum peak in this interval is * i want to keep it. if not i want to move on. i could do it for x(ii) or x(ii+1) but how to say
iff x(ii)<max(y)<x(ii+1)
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?
sorry sorry!
Y = [ 0.66 0.04 0.93 0.68 0.76 0.74 0.39 0.66 0.85 0.17];
x = [0 2 4 6 8 10]; %i want to make intervals based on x
x1 = [0 2 4 6 8];
x2 = [2 4 6 8 10];
First interval: 0-2, the Y values are 0.66 and 0.04. Let say Y* = 0.9 The maximum Y value in the interval of 0-2 < Y*
This means that in the new vectors x3,x4 0 and 2 will not be present. In the if statement i want to say that: if the maximum Y value in the interval is less than Y*, then for the new interval vectors i dont want to include that interval. sorry. is this better? thank you for patience!!
Following this, we want to have
x3 = [2];
x4=[4];

Sign in to comment.

Kelly Kearney
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);

Asked:

on 23 Aug 2012

Community Treasure Hunt

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

Start Hunting!