How to properly write an if-else-statement such that if the condition is met, then the values in x-vector will be stored in the vector; or else the values in the x-vector will not be deleted??

Hello,
I am stuck on trying to write what I think should be a simple and easy if-else statement, but I just seem to be having trouble connecting the dots...Lets says I have a column vector,
x = [2300:8:2458]';
and I would like to write an if-statement where each row (or element) in the x-vector will be looked at to see if the value meets these two conditions, for example,
if x >= 1 & x <=2440
"some statement goes here"
if it is true, then I would like to keep those values in the column vector, x, (or maybe create a new vector that contains the values that meet the if-condition). If not, I would like to delete those values from the x-vector, for example something like this,
else
x = [];
end
My guess is that it has something to do with the if-statement returning a logical value and I'm supposed to somehow write a statement that uses that logical value to help determine what values to store and what values to delete, or something like that...Or do I have to write a for loop outside of the if-statement and run it as many times as the length(x)?? I read somewhere that by using a single &, the if-statement looks at each element in the vector...but I'm not entirely sure. Can anyone please help?? I am using 2015a btw. Thanks!

 Accepted Answer

g = (x >= 1) & (x <= 2440); % Logical result of the elements you want to keep
x(~g) = []; % Delete the others (or you could make a new variable y = x(g) )
If you really want to use a loop for some reason, then my advice is to form g inside the loop, and then after the loop use g to delete the elements you don't want with the 2nd line shown above. Trying to delete elements inside the loop itself complicates things because the indexing is constantly changing and each element deletion causes a complete data copy which can seriously slow down that section of code.

6 Comments

Thanks for your quick response James. Your code appears to work just fine. But out of curiosity, how would you implement that into an if-else statement, using the example given, and output the same results as what your code provided??? That's what I'm stuck on trying to figure out, for practice purposes and to gain better familiarity with using if-else statements.
UPDATE to your response: Are you saying that in order to use an if-else statement to determine what values remain stored in the x-vector and what doesn't, I would have to create a for loop outside the if-else statement?? If not, do you mind providing a simple example on how to use an if-else statement to determine what values remain stored in the vector and what doesn't??
For example,
x = 1:1:10;
if x >= 1 & x <= 7
"some statement goes here"
else
"make the value = [] or some other statement that disregards those values"
end
x
E.g.,
g = false(size(x)); % Pre-allocate g
n = numel(x);
for k=1:n
% insert your if-else code here to test x(k) for some condition and set g(k)
end
x(~g) = [];
Just fill in the desired condition. The above code fills in the elements of g one at a time instead of using a vectorized one-liner.
"... Are you saying that in order to use an if-else statement to determine what values remain stored in the x-vector and what doesn't, I would have to create a for loop outside the if-else statement?? ..."
Yes, that is exactly it. The if-else code is inside of the for-loop construct. The if-else code would be testing one element of x at a time using the for-loop index.
Ok, sorry to bother you again James, but I think we're finally getting to my main question regarding this post, which is how to "insert the if-else code to test x(k) for some condition and set g(k)"? Would it be something like this? It says
Attempted to access x(10); index is out of bounds because numel(x)=8
That's what I have so far, I will continue working on it in the meantime..
UPDATED CODE!!!
x = [1:1:10]';
g = false(size(x)); % Pre-allocate g
n = numel(x);
for k=1:n
% insert your if-else code here to test x(k) for some condition and set g(k)
if x(k) >= 1 & x(k) <= 7
g(k) = 1;
else
g(k) = 0;
end
end
x(~g) = [];
I just updated the code by altering the statements as shown above, and I think I got it. THANKS JAMES TURSA.
Almost! The main problem is this part:
else
x(k) = [];
Each time that gets executed, the size of x shrinks by 1, so eventually the for loop index, which goes to n (the original size) exceeds the current size because of the shrinkage and you get the error.
Just delete this "else" part entirely. That is the whole point of remembering the logical result in the g variable ... so you don't have to shrink x inside the loop and worry about index limit changes (which can lead to errors as you have seen). The one line at the very end (outside the for-loop) does all of the deletions at once.
The other thing you need to fix is the setting of g(k). It should be:
g(k) = true;
ohh ok I see. kk got it. Thanks a bunch for the added explanation. I originally always thought that when writing an if-statement, you needed to at least include an "else" for the if-statement to compute; however in this case, it was not necessary because like you said, the logical results get stored in the logical g variable and is then deleted outside of the loop altogether.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!