Does anyone know what the operator ~= in Matlab? I have seen it in an if statement
if currsign ~= 0
[
]
Not equal to?
It simply means "not equal"
For example:
1 ~= 1 % Returns false
1 ~= 2 % Returns true
In Matlab ~= is not equal to. the brackets following the if are not valid Matlab syntax though. 'if' needs to be paired with an 'end'
Related
I am having some trouble understand why this is wrong
if x<=-1
elseif pwres=1
elseif -1<x<=1
pwres=x.^2
else
pwres=x.^3
end
I have been told not to write -1 in line #3.
Does MATLAB not support double inequality?
Also is it supposed to be x.^2 or x^2?
When you attempt -1<x<=1, MATLAB first calculates -1<x and returns 0 or 1 depending on the result. The 0 or 1 you end up with then gets compared to 1 using the <= operation, which would always return 1.
Effectively, you'd end up with a result of 1 for any value of x.
I just found the statement any('') returns a logical 0, while the statement all('') returns a logical 1.
If the function any does not think the empty string ('') as nonzero, the function all should do the same, but from the result, the function all seems to think the empty string ('') as nonzero.
BTW, the similar thing happens where any(NaN) returns logical 0 while all(NaN) returns logical 1.
Is it a MATLAB bug?
Here is the version information of the MATLAB I am using.
MATLAB Version: 9.1.0.441655 (R2016b)
MATLAB License Number: DEMO
According to the Documentation
definition of any:
any(x) ...determines if any element is a nonzero number or logical 1 (true)
In practice, any is a natural extension of the logical OR operator.
If A is an empty 0-by-0 matrix, any(A) returns logical 0 (false).
and definition of all:
all(x) ...determines if the elements are all nonzero or logical 1 (true)
In practice, all is a natural extension of the logical AND operator.
If A is an empty 0-by-0 matrix, then all(A) returns logical 1 (true).
We can implement both functions:
function out = Any(V)
out = false;
for k = 1:numel(V)
out = out || (~isnan(V(k)) && V(k) ~= 0);
end
end
function out = All(V)
out = true;
for k = 1:numel(V)
out = out && (V(k) ~= 0);
end
end
Explanation:
-In any we assume that all elements are not nonzero [so all are zeros] and we want to prove that the assumption is wrong so we provide an initial value of false.
-Because any is a natural extension of the logical OR operator we use ||
-Because we should check for nonzero numbers we use V(k) ~= 0
-Because we should check for nonzeros numbers and NaN is Not a Number we use ~isnan(V(k)).
-In all we assume that all elements are nonzero [so all are ones] and we want to prove that the assumption is wrong so we provide an initial value of true
-Because all is a natural extension of the logical AND operator we use &&
-Because we should check for nonzeros we use V(k) ~= 0
-Because the definition of all doesn't force that nonzero elements to be numbers we don't use ~isnan(V(k))
Any returns 0 because it is not the case that any of its elements are true. An any clause is true if any of its elements are true.
All returns 1 because it is the case that all of its elements are true. All of its elements are true so long as none of its elements are false, and none of its elements are false.
It is convenient and consistent w.r.t the rest of your math when applying an associative operation over an empty list of values returns the neutral element of that operation. This is why
a sum of zero numbers is 0
a product of zero numbers is 1
a logical or of zero booleans is false
a logical and of zero booleans is true
Keywords for further reading: monoid, fold.
I just found solid proof from the documentation about the any and all functions, as follows.
Documentation of the function any
https://www.mathworks.com/help/matlab/ref/any.html
If A is an empty 0-by-0 matrix, any(A) returns logical 0 (false).
Documentation of the function all
https://www.mathworks.com/help/matlab/ref/all.html
If A is an empty 0-by-0 matrix, then all(A) returns logical 1 (true).
And the empty string in MATLAB is actually a 0×0 empty char array (I just found it). That explains my original question, from the documentation.
My code works when I type
if size(k)==size(k1)
disp('They match');
end
or
if k-k1==0
disp('They match');
end
but if I type in two conditions at the same time like this,
if size(k)==size(k1) & k-k1==0
disp('They match');
end
I get an error saying
Matrix dimensions must agree.
Error in practice (line 32) if size(k)==size(k1) & k-k1==0
FYI, the dimension of k and k1 are both 1x717 double. I checked it.
So I want to make an if statement that includes two conditions at the same time, but I am experiencing an error. Two && won't work cuz two && is for scalars, but my k and k1 are vectors.
Any help will be appreciated
when you compare two vectors the result would be also a vector (a logical vector), but an if condition accept scalar logical values, so you can use all function.
if all(size(k)-size(k1)==0) && all(k-k1==0)
disp('They match');
end
You should always use && in a loop, '&' is used only for logical operation AND.
I have tested this and it works:
k = rand(1,10);
k1 = k;
if all(size(k)-size(k1)==0) && all(k-k1==0)
disp('They match');
end
because when you do this:
>> k-k1==0
ans =
1×10 logical array
1 1 1 1 1 1 1 1 1 1
So the if does not know which value to refer to. But when you do
>> all(k-k1==0)
ans =
logical
1
It gives a unique answer for all the elements of the vector.
Important Note:
Comparing numbers is not a good idea for making decisions on the loops, because of the Floating Point Error problem.
A Better War to Handle it
If you read about the floating point error problem, you will see that sometimes,
2.000 == 2.000 results to false. In order to fix that you can do as follows:
tolerance = 0.0001;
if all(size(k)-size(k1)==0) && all(abs(k-k1)<=tolerance)
disp('They match');
end
You first define an acceptable tolerance value depending on the nature of the problem you are trying to solve and then instead of comparing the subtract to zero, you compare the absolute value of the abstract to the tolerance. Thus, the numbers such as 23.0001 and 23.000 would be considered equal.
The problem is size(k) and size(k1) returns 1*2 vectors (number of rows and columns), so size(k)==size(k1) returns two values. On the other hand k-k1==0 returns only logical matrix with same dimension as k & k1.
For example, if k == k1, you would expect both to be equal.
size(k)==size(k1) % returns 1 1
k == k1 % returns 1
if [1 1] && 1 % gives erros
Alternatively, use isequal which will not give error even if dimensions not matching.
isequal(k,k1) % returns 1 if equal, 0 otherwise.
I have an array in Matlab called myVec and I have to execute an operation if the array contains at least one 1 and one 0.
I do not know how I could do that, I tried with find but it did not work.
This is what I need.
if %myVec contains 0 && myVec contains 1
%Code A
else
%Code B
end
I checked that if you try for example find(myVec==0)and it returned the positions which fulfill the statement, it could be used as a boolean if find(myVec==0) but then I tried if (find(myVec==0) && find(myVec==1)) and The following error is shown Operands to the || and && operators must be convertible to logical scalar values.
Thank you everyone.
if sum(myVec==1) && sum(myVec==0)
%Code A
else
%Code B
end
% sum(myVec==1) counts the number of ones in myVec
% sum(myVec==0) counts the number of zeros in myVec
% if myVec is a matrix with more than one rows and columns, use myVec(:) instead
I am writing an easy IF condition. The function is to judge each row of the matrix to be certain vectors. The code is as follows:
if (compareM(i,:)==[1, 0])||(compareM(i,:) ==[2, 1])
match_1 = match_1 +1;
else
mismatch_1 = mismatch_1 +1;
end
The error said ''Operands to the || and && operators must be convertible to logical scalar values''.
compareM is a n by 2 matrix and I wonder if the error is made by || operation. Thanks in advance!
If you are comparing vectors, not scalar values, you have to use | operator. As a result you get logical vector of element-by-element pairwise comparison. To use it in IF statement you have to convert either each logical statement (then use ||) or the result of | the to scalar with ALL, ANY or other function.
If you want to compare to vectors for equality use ISEQUAL function as
if isequal(compareM(i,:)==[1, 0]) || isequal(compareM(i,:)==[2, 1])
compareM(i, :) evaluates to a 1x2 numeric array, so compareM(i,:)==[1, 0] evaluates to a 1x2 logical array. The same for the expression to the right of the || sign. But you need a single logical value on each side of ||, not a 1x2 array of logical values.
If you want this expression to evaluate to true if both values on the lhs of == are the same as the corresponding elements on the rhs, wrap all() around each side:
all(compareM(i,:)==[1, 0]) || all(compareM(i,:) ==[2, 1])
if ((compareM(i,:)==[1, 0])||(compareM(i,:) ==[2, 1]))
match_1 = match_1 +1;
else
mismatch_1 = mismatch_1 +1;
end
Note the outer enclosing parentheses.