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??

4 views (last 30 days)
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

James Tursa
James Tursa on 5 Aug 2015
Edited: James Tursa on 5 Aug 2015
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
James Tursa
James Tursa on 5 Aug 2015
Edited: James Tursa on 5 Aug 2015
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;
George Vuong
George Vuong on 5 Aug 2015
Edited: George Vuong on 5 Aug 2015
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!