Testing Logic with More than 1 && Operator? - matlab

If I have a function with 2 inputs, is there a way to test both its range other than nested if functions?
For example...
if input1 > 0 && input1 <10 && input2 > 0 && input2 <10
this doesn't work so I'm forced to write
if input1 > 0 && input1 <10
if input2 > 0 && input2 <10
'%something
end
end
Are there easier ways to test range of multiple inputs?

I don't know of a way to avoid nested if functions, but you can avoid repetition:
In JavaScript:
function test(argument1,argument2) {
check = 0;
for (i=0;i<arguments.length;i++){
if (arguments[i] > 0 && (arguments[i] < 10)) {
check++;
if (check === arguments.length) {
//do something
}
}
}
}
test(1,2);

Try this in Matlab:
find((input1==1:20),1) & find((input2==50:100),1)

Related

Group typo3 condition

I need such a condition in ts:
([treeLevel = 0] && [globalVar = GP:R > 0]) || [PIDinRootline = {$pages.2018}]
I wanna show block if page has treelevel=0 and the get var R > 0, or if page id = $pages.2018
It looks like the similar code in php:
if(($treeLevel == 0 && $r > 0) || (pid == number))
The all expression in first brackets should be right, or in second.
Is it exist the method to group it like the previous record or I can only use userfunc?
There is no grouping in TS conditions, but if you need this particular condition from your post I think it is not needed because brackets around && are useless in this case.
(p && q) || r
is exactly the same as
p && q || r
Did you tested it?

Sum of Printed For Loop in Swift

For a project, I'm trying to find the sum of the multiples of both 3 and 5 under 10,000 using Swift. Insert NoobJokes.
Printing the multiples of both 3 and 5 was fairly easy using a ForLoop, but I'm wondering how I can..."sum" all of the items that I printed.
for i in 0...10000 {
if i % 3 == 0 || i % 5 == 0 {
print(i)
}
}
(468 individual numbers printed; how can they be summed?)
Just a little walk through about the process. First you will need a variable which can hold the value of your sum, whenever loop will get execute. You can define an optional variable of type Int or initialize it with a default value same as I have done in the first line. Every time the loop will execute, i which is either multiple of 3 or 5 will be added to the totalSum and after last iteration you ll get your result.
var totalSum = 0
for i in 0...10000 {
if i % 3 == 0 || i % 5 == 0
{
print(i)
totalSum = totalSum + i
}
}
print (totalSum)
In Swift you can do it without a repeat loop:
let numberOfDivisiblesBy3And5 = (0...10000).filter{ $0 % 3 == 0 || $0 % 5 == 0 }.count
Or to get the sum of the items:
let sumOfDivisiblesBy3And5 = (0...10000).filter{ $0 % 3 == 0 || $0 % 5 == 0 }.reduce(0, {$0 + $1})
range : to specify the range of numbers for operation to act on.
here we are using filter method to filter out numbers that are multiple of 3 and 5 and then sum the filtered values.
(reduce(0,+) does the job)
let sum = (3...n).filter({($0 % 3) * ($0 % 5) == 0}).reduce(0,+)
You just need to sum the resulting i like below
var sum = 0
for i in 0...10000 {
if i % 3 == 0 || i % 5 == 0 {
sum = sum + i
print(i)
}
}
Now sum contains the Sum of the values
Try this:
var sum = 0
for i in 0...10000 {
if i % 3 == 0 || i % 5 == 0 {
sum = sum + i
print(i)
}
}
print(sum)
In the Bottom line, this should to be working.
var sum = 0
for i in 0...10000 {
if i % 3 == 0 || i % 5 == 0 {
sum += i
print(i)
}
}
print(sum)

Nyquist criterion - undefined variable in algorithm

I write an algorithm that checks the stability of the closed system of the Nyquist criterion (http://en.wikipedia.org/wiki/Nyquist_stability_criterion)
function answear=stability(re,im)
%% Function check stability of system
%re is real part of transmitation
%im is imagine part of transmitation
%% Check number of vectors elements
re(end +1:5) = 0;
im(end +1:5) = 0;
if( length(re) > length(im))
root = length(re);
else
root = length(im);
end
for w=1:root
tran(w) = re(1) + re(2)*w.^1 + re(3)*w.^2 + re(4)*w.^3 + re(5)*w.^4 +1i*(...
im(1) + im(2)*w.^1 + im(3)*w.^2 + im(4)*w.^3 +im(5)*w.^4);
end
%% Algorithm
switch root
case 0
exist('Write nonzero numbers', 'var')
case 1
for w=1:length(w)
if( real(tran(w)) > 0 && imag(tran(w)) > 0)
answear=1;
else
answear=0;
end
end
case 2
for w=1:length(w)
if( real(tran(w)) > 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) > 0)
answear=1;
else
answear=0;
end
end
end
case 3
for w=1:length(w)
if( real(tran(w)) > 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) < 0)
answear=1;
else
answear=0;
end
end
end
end
case 4
for w=1:length(w)
if( real(tran(w)) > 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) < 0)
if( real(tran(w)) > 0 && imag(tran(w)) < 0)
answear=1;
else
answear=0;
end
end
end
end
end
end
%% Answear
if answear==1
disp('System unstable')
else
disp('System stable')
end
plot(real(tran),imag(tran))
grid on
end
Function returns
Undefined function or variable "answear".
Error in stability (line 87) if answear==1
So the algorithm is badly written?
Your code could use a lot of cleanup:
Instead of an if-statement such as this one:
if( real(tran(w)) > 0 && imag(tran(w)) > 0)
answear=1;
else
answear=0;
end
you could write a boolean assignment:
answear = real(tran(w)) > 0 & imag(tran(w)) > 0);
Why do you have three (almost) identical nested if-statements at all?
if( real(tran(w)) > 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) < 0)
First of all, you could replace everything with one if-statement. But what are you actually testing with this? It seems that those nested if statements are never executed. For instance, real(tran(w)) cannot be both positive and negative at the same time (unless it is a vector you are working on, in which case you shouldn't be using the operator &&).
Also, this is probably your code triggers the error regarding variable answear. Accessing it is impossible since it hasn't been assigned a value (none of the if-statements have been executed).
What is tran, and what is w? Are they global variables? If they are, pass them as input parameters. Your function is probably poorly designed if it relies on external states and variables.
I haven't actually run your code, but these suggestions should make it easier for you to debug it.
P.S:
Please fix the annoying spelling error (it is "answer" and not "answear") :)

How do I add a condition to an existing conditional expression?

I had a programmer write a Perl script for my site.
One of the functions is to update price/stock when a certain condition is met.
# update when price/stock conditions met
if ( ($force_price_updates == 1) ||
($data->{'price'} <= $product_price && $data->{'quantity'} > 0) ||
($product_quantity == 0 && $data->{'quantity'} > 0) ) {
What the above is not doing is not updating the price if the new price is higher. It updates the stock value, but if the new stock comes at a higher price, I lose out. Stock gets updated and but the price is not.
The script goes through a number of feeds and if the same product is found in any of the feeds, the script should amend price/stock change according to the rule above.
I can't find the programmer and my Perl knowledge is limited. I understand what the code is doing, but don't know what it should do if the price is higher and stock is greater than zero.
You can add the extra condition you're looking for to that statement.
The condition you're looking to match is:
$data->{'price'} > $product_price && $product_quantity > 0
So the final version would look like this:
if (($force_price_updates == 1) || ($data->{'price'} <= $product_price && $data->{'quantity'} > 0) || ($product_quantity == 0 && $data->{'quantity'} > 0) || ($data->{'price'} > $product_price && $product_quantity > 0)) {

multiple instance if statement in xcode (iphone)

what is the proper way to do If statement with two variable in Xcode for Iphone
currently I have
if (minute >0) && (second == 0) {
minute = minute - 1;
second = 59;
}
The same was you would do it in any C/C++/Objective-C compiler, and most Algol derived languages, and extra set of parenthesis in order to turn to seperate boolean statements and an operator into a single compound statement:
if ((minute > 0) && (second == 0)) {
minute = minute - 1;
second = 59;
}
You'll need another set of parenthesis:
if ((minute >0) && (second == 0)) {
minute = minute - 1;
second = 59;
}
Or you could also write:
if (minute > 0 && second == 0)
Which is what you'll start doing eventually anyway, and I think (subjective) is easier to read. Operator precedence insures this works...