Qlikview - Count IF A = X AND B != Y OR Z - logic

As the title says, I'm trying to get a count of data that follows the logic above. What I have so far;
=COUNT(IF(A='X', A AND IF(B <> 'Y' OR B <> 'Z', B)))
I'm aware I could do a set analysis, but A) doing so makes it so that selecting a different value of A still shows all data where A = X and B) I want to figure this out.

First, I'd encourage using set analysis. If you're going this route, however, I think that it's easier to read if you switch to using the Sum function:
Sum(If(A='X' AND (B <> 'Y' OR B <> 'Z'), 1, 0))

Related

PROLOG Asignation problems

I have to solve a problem involving the WWESupercard game. The idea is to to take 2 cards and make them fight, comparing each stat alone and every combination, making 10 comparations in total. A fighter wins if it gets more than 50% of the fights (6/10).
The problem I have is that the "is" statement is not working for me.
This are some of the "solutions" I've tried so far, using only the base stats and no combinations for faster testing, none of them are working as I intend:
leGana(X,Y) :- T is 0,
((A is 0, (pwr(X,Y) -> A1 is A+1));
(B is 0, (tgh(X,Y) -> B1 is B+1));
(C is 0, (spd(X,Y) -> C1 is C+1));
(D is 0, (cha(X,Y) -> D1 is D+1))),
T1 is T+A1+B1+C1+D1, T1 > 2.
leGana(X,Y) :- T is 0,
((pwr(X,Y) -> T is T+1);
(tgh(X,Y) -> T is T+1);
(spd(X,Y) -> T is T+1);
(cha(X,Y) -> T is T+1)),
T1 > 2.
I've tried other ways, but I keep getting the same 2 error, most of the times:
It fails on "T is T+1" saying "0 is 0+1" and failing
On example 1, it does the "A1 is A+1", getting "1 is 0+1", but then it jumps directly to the last line of the code, getting "T1 is 0+1+_1543+_1546 ..."
Also, sorry if the way I wrote the code is not correct, it's my first time asking for help here, so any advice is welcome.
Prolog is a declarative language, which means that a variable can only have a single value.
If you do this:
X is 1, X is 2.
it will fail because you're trying to say that X has both the value 1 and the value 2, which is impossible ... that is is is not assignment but a kind of equality that evaluates the right-hand side.
Similarly
X = 1, X = 2.
will fail.
If you want to do the equivalent of Python's
def f(x):
result = 1
if x == 0:
result += 1
return result
then you need to do something like this:
f(X, Result) :-
Result1 = 1,
( X = 0
-> Result is Result1 + 1
; Result = Result1
).

How can I emulate the results of this if then then statement while using correct syntax?

Working on an exercise for university class and cant seem to represent what I am trying to do with correct syntax in ocaml. I want the function sum_positive to sum all the positive integers in the list into a single int value and return that value.
let int x = 0 in
let rec sum_positive (ls: int list) = function
|h::[] -> x (*sum of positive ints in list*)
|[] -> 0
|h::t -> if (h >= 0) then x + h then sum_positive t else sum_positive t (*trying to ensure that sum_positive t will still run after the addition of x + h*)
On compiling I am met with this error,
File "functions.ml", line 26, characters 34-38:
Error: Syntax error
This points to the then then statement I have in there, I know it cannot work but I cant think of any other representations that would.
You have if ... then ... then which is not syntactically valid.
It seems what you're asking is how to write what you have in mind in a way that is syntactically valid. But it's not clear what you have in mind.
You can evaluate two expressions in OCaml sequentially (one after the other) by separating them with ;. Possibly that is what you have in mind.
However it seems to me your code has bigger problems than just syntax. It appears you're trying to use x as an accumulated sum for the calculation. You should be aware that OCaml variables like x are immutable. Once you say let x = 0, the value can't be changed later. x will always be 0. The expression x + h doesn't change the value of x. It just evaluates to a new value.
The usual way to make this work is to pass x as a function parameter.
I was getting an issue that had involved the parameter of , I believe it was because I was trying to add an int value to function of type int list. This is what I ended up with.
let rec sum_positive = function
|[] -> 0
|h::t -> if h > 0 then h + (sum_positive t) else sum_positive t
a lot simpler than I thought it out to be.

Collection of variable-length Tuples AND map with multiple values TO weighted combinations

This problem is from "Functional Programming Principles in Scala" # Coursera so I need to avoid having complete code in this question - it's past deadline already but there are always years to come. I was looking for general suggestions on methods to implement this transformation.
I have a set of variable-length Tuples, full subset of some String in lowercase
val indexes = Set('a', 'b', 'c')
and a set of tuples with max allowed occurence for each char
val occurences = Set(('a', 1), ('b', 5), ('c', 2))
I want to get combinations of weighted tuples:
val result = Seq(Set(), Set((a, 1)), Set((b, 1)), Set((b, 2)) ... Set((a, 1)(b, 2)(c, 2)) ...)
My assignment suggests easy way of building result via recursive iteration.
I'd like to do it in more "structural?" way. My idea was to get all possible char subsets and multiplex those with added weights (~pseudocode in last line of post).
I got subsets via handly subsets operator
val subsets = Seq(Set(), Set(a), Set(b), Set(c), Set(a, b), Set(a, c), Set(b, c), Set(a, b, c)
and also map of specific Int values for each char, either of
val weightsMax Map(a -> 1, b -> 5, c -> 2)
val weightsAll Map(a -> List(1), b -> List(5,4,3,2,1), c -> List(2,1))
I don't really know which language feature I should use for this operation.
I know about for and collection operations but don't have experience to operate with those on this level as I'm new to functional paradigm (and collection operations too).
I would have no problems with making some corporate-styled java / XML to solve this (yeah ...).
I'd like to have something similar defined:
FOREACH subset (MAP chars TO (COMBINATIONS OF weights FOR chars))
You can express this problem recursively and implement it this exact way. We want to build a function called expend: Set[Char] => List[Set[(Char, Int)]] that returns all the possible combinations of weights for a set of chars (you wrote it chars TO (COMBINATIONS OF weights FOR chars)). The intuitive "by the definition" way is to assign each possible weights to the first char, and for each of these assign each possible weights to the second char and so on...
def expend(set: Set[Char]): List[Set[(Char, Int)]] =
if(set isEmpty) Nil else
allPairsFromChar(set head) flatMap (x => expend(set tail) map (_ + x))
Where allPairsFromChar is trivial from your weightsAll and your FOREACH subset (...) is another flatMap ;)

Getting subset of MATLAB vector

I have got a vector in MATLAB that represents function values. I am plotting these, but want to highlight the ones that are in a specific range. In particular I am interested in all points with function value close to, but less than 'v'. I.e. for a value 'x', I want to highlight this point if
abs( x - v ) < epsilon && x < v
If I want to select all the points 'x' such that
abs( x - v) < epsilon
what I have got to work is (I am not sure if this is good coding practice or not)
inds = (abs( xs - v ) < epsilon ) ;
and then plot the xs against my ys in axes a
plot( a, ys(inds), xs(inds), 'ks ' ) ;
This approach is no longer working if I try and do
inds = (abs( xs - v) < epsilon && xs < v ) ;
In this case I am getting the following error, no matter how I arrange the brackets:
'Operands to the || and && operators must be convertible to logical scalar values.'
I guess I have two questions
1.) Why does the approach not work if I try and use the logical &&? As far as I can see I am using expressions that can be converted to logical scalar values
2.) Is this a good way to select a subset of points in MATLAB?
Thanks,
Keeran
That's because && is only for scalar values. You have vectors, not scalars, so you have to use & instead:
inds = ( abs(xs-v) < epsilon & xs < v );
Other than that yes, it is the right way to select a subset of points.
For reference, note that & also works for scalars. The reason to have && is that it is potentially faster for scalars (see doc).

Reduction from Atm to A (of my choice) , and from A to Atm

Reduction of many one , is not symmetric . I'm trying to prove it but it doesn't work
so well .
Given two languages A and B ,where A is defined as
A={w| |w| is even} , i.e. `w` has an even length
and B=A_TM , where A_TM is undecidable but Turing-recognizable!
Given the following Reduction:
f(w) = { (P(x):{accept;}),epsilon , if |w| is even
f(w) = { (P(x):{reject;}),epsilon , else
(Please forgive me for not using Latex , I have no experience with it)
As I can see, a reduction from A <= B (from A to A_TM) is possible , and works great.
However , I don't understand why B <= A , is not possible .
Can you please clarify and explain ?
Thanks
Ron
Assume for a moment that B <= A. Then there is a function f:Sigma*->Sigma* such that:
f(w) = x in A if w is in B
= x not in A if w is not in B
Therefore, we can describe the following algorithm [turing machine] M on input w:
1. w' <- f(w)
2. if |w'| is even return true
3. return false
It is easy to prove that M accepts w if and only if w is in B [left as an exercise to the reader], thus L(M) = B.
Also, M stops for any input w [from its construction]. Thus - L(M) is decideable.
But we got that L(M) = B is decideable - and that is a contradiction, because B = A_TM is undecideable!

Resources