Clear Filters
Clear Filters

1 if true, 0 if false: analyzing data from excel with text condition

5 views (last 30 days)
Hi all,
I'm looking to use the csv file I've uploaded to tell me at what points certain conditions are true. Allow me to explain:
For all points that do not have a q or h in column B, then the GoodBad vector will get a '0'
The B column is for reference. I want to know if the value of A column falls in these conditions: A <= 30, A >= 20 while the B column reads 'q'. If it is, I want to add a '1' to my GoodBad vector; if the A column value does not fall in to the condition I've mentioned, then the GoodBad vector would get a '-1'.
The same idea for the 'h' in the B column with the exception that the condition would be: A >= 40, A <= 50. Furthermore; While the A value is less than or equal to 50, and greater than or equal to 40, and ALSO while the B column reads 'h' at the respective lines, then the GoodBad vector will be a '1' at the respective line if the condition is true, and '-1' if that condition is false.
I know this question is a little difficult to understand, so if you need clarification, please comment and I'll try to make it easier to see what I'm asking for.
GoodBad = []
if B =~ 'q'
GoodBad = [GoodBad, 0]
if B =~ 'h'
GoodBad = [GoodBad, 0]
if B = 'q'
if 20 <= A <= 30
GoodBad = [GoodBad, 1]
else
GoodBad = [GoodBad, -1]
end
end
if B = 'h'
if 40 <= A <= 50
GoodBad = [GoodBad, 1]
else
GoodBad = [GoodBad, -1]
end
end
  3 Comments
Dylan Mecca
Dylan Mecca on 8 Mar 2018
My code is terrible lol, so my explanation is more accurate to what I'm looking for.
Rik
Rik on 12 Mar 2018
Did either answer help you? If so, please accept the answer that solves your problem best, or comment on what is still incorrect.

Sign in to comment.

Answers (2)

Jan
Jan on 8 Mar 2018
Edited: Jan on 8 Mar 2018
Maybe you mean:
if B == 'q' % Not: B = 'q'
if 20 <= A && A <= 30 % NOT: 20 <= A <= 30!!!
GoodBad = [GoodBad, 1]
else
GoodBad = [GoodBad, -1]
end
elseif B == 'h' % Not B = 'h'
if 40 <= A && A <= 50 % NOT: 40 <= A <= 50!!!
GoodBad = [GoodBad, 1]
else
GoodBad = [GoodBad, -1]
end
else
GoodBad = [GoodBad, 0]
end
The expression 40 <= A <= 50 is evaluated from left to right:
  1. 40 <= A. This is TRUE or FALSE, an interpreted as numerical value 1 or 0 depending on the value of A.
  2. 1 <= 50 or 0 <= 50. This is TRUE independent from the value of A.
You have to split this to two comparisons:
40 <= A && A <= 50
B='q' assigns the character 'q' to the variable B. But you want to compare the values, so you need == instead of =.
Even shorter code:
v = (B == 'q') * (2*(20 <= A && A <= 30) - 1) + ...
(B == 'h') * (2*(40 <= A && A <= 50) - 1);
GoodBad = [GoodBad, v]
By the way: If GoodBad is growing iteratively, this consumes a lot of resources. Remember that if the final vector has 1000 elements, you need memory for sum(1:1000) elements temporarily. Better create GoodBad with the final size using zeros() and set the elements using a loop counter. Search in this forum for "pre-allocation".

Rik
Rik on 8 Mar 2018
Another possibility of what you might mean is the code below. This also shows how intuitive logical indexing can work. Note that && evaluates in a scalar context (and doesn't evaluate the second part if the first is false), while & is the or function (so it works on vectors as well).
GoodBad=zeros(size(B));%initialize to an empty vector
cond= A<=30 & A>=20 ;
GoodBad(cond & B=='q')=1;
GoodBad(~cond & B=='q')=-1;
cond= A<=50 & A>=40 ;
GoodBad(cond & B=='h')=1;
GoodBad(~cond & B=='h')=-1;

Categories

Find more on Eigenvalues in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!