How do you get "1" as integer in if-else statement rather than as boolean in MATLAB? - 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

Related

what is mean by swift condition is if ((status & 0x3F) == 1 ){ }

if ((status & 0x3F) == 1 ){ }..
the status is variable in swift language.
what is mean about this condition, & mean and (status & 0x3F) value return
& is the bitwise AND operator. It compares the bits of the two operands and sets the corresponding bit to 1 if it is 1 in both operands, or to 0 if either or both are 0.
So this statement:
((status & 0x3F) == 1)
is combining status with 0b111111 (the binary equivalent of 0x3F and checking if the result is exactly 1. This will only be true if the last 6 bits of status are 0b000001.
In this if:
if( (dtc24_state[2] & 0x8) == 0x8 ) {
self.haldexABCDTC24State.text = status_str + " - UNKNOWN"
self.haldexABCDTC24State.textColor = text_color
active_or_stored_dtc = true
}
dct24_state is an array of values. The value of dct24_state[2] is combined with 0x8 or 0b1000 and checked against 0x8. This is checking if the 4th bit from the right is set. Nothing else matters. If the 4th bit from the right is set, the if is true and the code block is executed.
0x3F is 111111. So, it means this:
for each bit of yourNumber in binary system presentation use and method.
This way truncates the left part of the number. and the result compares with 1.
e.g.
7777 is 1111001100001 after executing and this number converts into
100001. So the result is false.
But for 7745 (1111001000001) the result is 1. The result is true.
The rule for 'and' function: 0 & 0 = 0 ; 0 & 1 = 0; 1 & 0 = 1; 1 & 1 = 1.

Increment variable for if statement

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

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.

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

Creating a for-loop that stores values in a new variable

I'm quite new to Matlab so excuse me for the basic question.
I need to make a for-loop that repeats it's self 384 times.
So :
for i=1:384
I now need the for loop to check if 2 certain variables have the value 1 through 10, and then let them store this in a new variable with that value.
So:
if x==1
somevariable = 1
elseif x== 2
saomevariable = 2
..
..
..
elseif y = 1
someothervariable = 1
etc etc.
Is there a way to write this more efficient?
Thank you!
The first think you can do is:
if(x >= 1 && x <= 10)
somevariable=x;
end
if(y >= 1 && y <= 10)
someohtervariable=y;
end
If you could post more information about "x" and "y", perhaps your script can be further "improved".
Hope this helps.