필터 지우기
필터 지우기

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

조회 수: 5 (최근 30일)
Dylan Mecca
Dylan Mecca 2018년 3월 8일
댓글: Rik 2018년 3월 12일
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
Dylan Mecca
Dylan Mecca 2018년 3월 8일
My code is terrible lol, so my explanation is more accurate to what I'm looking for.
Rik
Rik 2018년 3월 12일
Did either answer help you? If so, please accept the answer that solves your problem best, or comment on what is still incorrect.

댓글을 달려면 로그인하십시오.

답변 (2개)

Jan
Jan 2018년 3월 8일
편집: Jan 2018년 3월 8일
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 2018년 3월 8일
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;

카테고리

Help CenterFile Exchange에서 Eigenvalues에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by