Increment variable for if statement - matlab

Is there a more elegant way to write this? I dont want to use a for loop
if i==1 || i==6 || i==11 || i==16 || i==21 || i==26 || i==31 || i==36
function
end
basically i is the index of a vector, after each fifth element of this vector (starting with the first ) a specific function is applied. i starts with 1 and it increments after the if statement and just if it equals these values of the if condition the if statement is valid

EDITTED FOR MATLAB CODE OF MODULO
output = mod(input, 5); //this will output 1 if it is 1, 5, 11, 16
//input is your 1, 5, 11, 16 etc
//output is the result of modulo. else it is 0, 2, 3, 4
if(output == 1)
[previous answer]
i forgot how to write this in matlab but with your values, put it this way.
if(number%5==1)
any input 1 or 6 or 11 or any else that can add 5 to it, you'll end up in 1. else it will return false

Related

How do you get "1" as integer in if-else statement rather than as boolean in MATLAB?

I am trying to use MATLAB FUNCTION block in simulink .
The model is shown below
In the "time_calc" Function i want to manipulate the variable "Sector" as shown in the code below
if sector == 1 || 2
sec = 1
elseif sector == 3 || 4
sec = 2
elseif sector == 5||6
sec = 3
elseif sector == 7||8
sec = 4
elseif sector == 9||10
sec = 5
elseif sector == 11 || 12
sec = 6
end
The below is the scope and you can see the values of "sector" changing from 0 through 12 and then repeating itself
But I am getting the value of "sec" as constant "1"(shown below in figure)
(Maybe because it is evaluating the first "1" as boolean true and running that statement only over and over again)
How to correct it ?
if sector == 1 || 2 evaluates sector == 1, if it's true, the statement is true. If it's false, it evaluates 2, which is always true, and so the statement is always true.
What you intended to write was if sector == 1 || sector == 2. You can also write this as if any(sector == [1, 2]).
Your function is equivalent to:
sec=ceil(sector/2)
#Cris Luengo's answer shows why your code is wrong. But I suggest you change the entire thing by this one liner, that is much clearer.
Remove the elseif's and replace them all with just if's

Conditional "Or" Statements in MATLAB

I am under the impression that || is the "or" statement in MATLAB. Perhaps someone can explain the confusing behaviour I am seeing:
a = 2;
a == 2 %returns ans = 1 (true)
a == 2 || 3 %returns ans = 1 (true)
a == 3 || 4 %returns ans = 1 (true)??!!
What am I missing here? 'a' is neither 3 or 4, so shouldn't
a == 3 || 4
return ans = 0 (false)?
The expression
a == 3 || 4
is evaluated that way :
a == 3 => false
then
false || 4 => true
if you want to check whether a is equal to 3 or 4 you should write
(a == 3) || (a == 4)
which is evaluated that way
a == 3 => false
then
a == 4 => false
then
false || false => false
Thomas's answer is an excellent explanation of what's going on here; another way that you can compare a variable to multiple answers is using the any() function.
solutions = [3 4];
any(a==solutions);
The a==solutions line creates a matrix the same size as solutions, which contains 1's in indecies which where the conditional is true, and 0's where it is false.
A few more examples:
any(isprime([17:24])); %returns true; 17, 19 and 23 are prime
any(isinteger(sqrt([17:24]))); %(test for square number) returns false; no square numbers in this range
any(mod(magic(3)(:),6)==3); %returns true; 9 mod 6 == 3. Note (:) inserted so that any is evaluated against all entries of the square matrix created by magic
a == 3 || 4 %returns ans = 1 (true)??!!
The reason for the above behaviour is due to the fact that any real number other than '0' in MATLAB is always evaluated to true.
So what is happening here is
The expression a == 3 is evaluated first and found to be false.
Next, expression false || 4 is evaluated.
Since '4' is a real number other than zero, the resulting expression is false || true which is evaluated to true.
To get a desire result use (a == 3) || (a == 4) which is evaluated as false || false which returns false.

Why do some values not function for my code?

I am trying to create an app that will have the user input a number and the app will determine if it is prime or not. Here is my code:
var arr: [Int] = [2, 3, 4, 5, 6, 7, 8, 9]
let text = Int(textField.text!)!
for x in arr {
if text == 1 {
iLabel.text = "NOT PRIME!"
}
else if x == 2 || x == 3 || x == 5 || x == 7 {
iLabel.text = "IT'S PRIME!"
}
else if text % x == 0 {
iLabel.text = "NOT PRIME!"
break
}
else if text % x != 0 {
iLabel.text = "IT'S PRIME!"
break
}
}
For the most part, this works. However, when I set variable "text" to equal something like 82, the result is "IT'S PRIME!" despite it being evenly divisible by 2...can anyone explain the flaw in my code?
if text == 1 else if x == 2 else if text % x == 0
^ ^ ^
it will trigger x=2 and set "IT'S PRIME` before it gets to the modulo/remainder check, which it will then skip. It will trigger the same thing at loop 3, 5, 7.
82 is not divisible by 8 or 9 so it won't happen to trigger the remainder check at the end of the loop and accidentally output the correct value, leaving "IT'S PRIME" in the output box, without actually checking whether it's prime.

Check a multiple in Swift?

I am trying to find the odd numbers and a multiple of 7 between a 1 to 100 and append them into an array. I have got this far:
var results: [Int] = []
for n in 1...100 {
if n / 2 != 0 && 7 / 100 == 0 {
results.append(n)
}
}
Your conditions are incorrect. You want to use "modular arithmetic"
Odd numbers are not divisible by 2. To check this use:
if n % 2 != 0
The % is the mod function and it returns the remainder of the division (e.g. 5 / 2 is 2.5 but integers don't have decimals, so the integer result is 2 with a remainder of 1 and 5 / 2 => 2 and 5 % 2 => 1)
To check if it's divisible by 7, use the same principle:
if n % 7 == 0
The remainder is 0 if the dividend is divisible by the divisor. The complete if condition is:
if n % 2 != 0 && n % 7 == 0
You can also use n % 2 == 1 because the remainder is always 1. The result of any mod function, a % b, is always between 0 and b - 1.
Or, using the new function isMultiple(of:, that final condition would be:
if !n.isMultiple(of: 2) && n.isMultiple(of: 7)
Swift 5:
Since Swift 5 has been released, you could use isMultiple(of:) method.
In your case, you should check if it is not multiple of ... :
if !n.isMultiple(of: 2)
Swift 5 is coming with isMultiple(of:) method for integers , so you can try
let res = Array(1...100).filter { !$0.isMultiple(of:2) && $0.isMultiple(of:7) }
Here is an efficient and concise way of getting the odd multiples of 7 less than or equal to 100 :
let results: [Int] = Array(stride(from: 7, through: 100, by: 14))
You can also use the built-in filter to do an operation on only qualified members of an array. Here is how that'd go in your case for example
var result = Array(1...100).filter { (number) -> Bool in
return (number % 2 != 0 && number % 7 == 0)
}
print(result) // will print [7, 21, 35, 49, 63, 77, 91]
You can read more about filter in the doc but here is the basics: it goes through each element and collects elements that return true on the condition. So it filters the array and returns what you want

Why doesn't this coffee-script print out?

So when the index is 0, I want to print it out:
a = [ 1, 2, 3 ]
for i of a
if i == 0
console.log a[i]
But there is no output.
i == 0 is never true...
i returns the index as a string, if you parse them as an integer, it would work
a = [ 1, 2, 3 ]
for i of a
if parseInt(i) == 0
console.log a[i]
It's because i will only be 1, 2 or 3, as you loop over the items in a, not the index numbers.
This works the way you described above:
a = [ 1, 2, 3 ]
for i in [0..a.length]
if i == 0
console.log a[i]
You shouldn't use of to loop over an array, you should use in. From the fine manual:
Comprehensions can also be used to iterate over the keys and values in an object. Use of to signal comprehension over the properties of an object instead of the values in an array.
yearsOld = max: 10, ida: 9, tim: 11
ages = for child, age of yearsOld
"#{child} is #{age}"
So you're trying to iterate over the properties of an array object, not its indexes.
You should use one of these for your loop:
for e, i in a
if(i == 0)
console.log(a[i])
for e, i in a
console.log(e) if(i == 0)
console.log(e) for e, i in a when i == 0
#...
Or, since you have an array and a numeric index, why not just skip the loop and get right to the point:
console.log(a[0])