Consider the following code
t = ones(3,5)
Ind2save = find(t(1,:) == 0,1,'first')
So for example I am trying to find if even the first zero of the first row, so if the first element is a non zero then
if(Ind2save ~= 1 )
disp('no')
end
now for the above condition it doesn't display 'no' because the condition is not fulfilled but because all the rows are filled and Ind2save is an empty matrix so we another condition to check if it is fully filled then
if(Ind2save > 1 || isempty(Ind2save))
disp('no')
end
I get the following error
Operands to the || and && operators must be convertible to logical scalar values.
I searched for the reasons due to which this error is caused and in majority of the cases people were comparing two vectors so a better idea was to replace || with | but in my case the conditions are never vectors but Ind2save > 1 returns an empty matrix , does anyone know why is the reason for that? How can I accommodate both the conditions?
The issue is because in your case Ind2save is empty ([]) therefore the first part of your condition can't be used with || since [] > 1 doesn't yield a logical scalar (it results in []).
In order to fix this, you can to flip the order of your conditions such that you check if the array is empty first.
if isempty(Ind2save) || Ind2save > 1
The reason that this works is that if Ind2Save is empty, then the first condition evaluates to true therefore short-circuiting the rest of the checks.
You may have other issues if for some reason Ind2save is a vector. In that case you could need to so something to convert it to a logical scalar:
if isempty(Ind2save) || ismember(1, Ind2save)
What is the difference between the | and || logical operators in MATLAB?
I'm sure you've read the documentation for the short-circuiting operators, and for the element-wise operators.
One important difference is that element-wise operators can operate on arrays whereas the short-circuiting operators apply only to scalar logical operands.
But probably the key difference is the issue of short-circuiting. For the short-circuiting operators, the expression is evaluated from left to right and as soon as the final result can be determined for sure, then remaining terms are not evaluated.
For example, consider
x = a && b
If a evaluates to false, then we know that a && b evaluates to false irrespective of what b evaluates to. So there is no need to evaluate b.
Now consider this expression:
NeedToMakeExpensiveFunctionCall && ExpensiveFunctionCall
where we imagine that ExpensiveFunctionCall takes a long time to evaluate. If we can perform some other, cheap, test that allows us to skip the call to ExpensiveFunctionCall, then we can avoid calling ExpensiveFunctionCall.
So, suppose that NeedToMakeExpensiveFunctionCall evaluates to false. In that case, because we have used short-circuiting operators, ExpensiveFunctionCall will not be called.
In contrast, if we used the element-wise operator and wrote the function like this:
NeedToMakeExpensiveFunctionCall & ExpensiveFunctionCall
then the call to ExpensiveFunctionCall would never be skipped.
In fact the MATLAB documentation, which I do hope you have read, includes an excellent example that illustrates the point very well:
x = (b ~= 0) && (a/b > 18.5)
In this case we cannot perform a/b if b is zero. Hence the test for b ~= 0. The use of the short-circuiting operator means that we avoid calculating a/b when b is zero and so avoid the run-time error that would arise. Clearly the element-wise logical operator would not be able to avoid the run-time error.
For a longer discussion of short-circuit evaluation, refer to the Wikipedia article on the subject.
Logical Operators
MATLAB offers three types of logical operators and functions:
| is Element-wise — operate on corresponding elements of logical arrays.
Example:
vector inputs A and B
A = [0 1 1 0 1];
B = [1 1 0 0 1];
A | B = 11101
|| is Short-circuit — operate on scalar, logical expressions
Example:
|| : Returns logical 1 (true) if either input, or both, evaluate to true, and logical 0 (false) if they do not.
Operand: logical expressions containing scalar values.
A || B (B is only evaluated if A is false)
A = 1;
B = 0;
C =(A || (B = 1));
B is 0 after this expression and C is 1.
Other is, Bit-wise — operate on corresponding bits of integer values or arrays.
reference link
|| is used for scalar inputs
| takes array input in if/while statements
From the source:-
Always use the && and || operators when short-circuiting is required.
Using the elementwise operators (& and |) for short-circuiting can
yield unexpected results.
Short-circuit || means, that parameters will be evaluated only if necessarily in expression.
In our example expr1 || expr2 if expr1 evaluates to TRUE, than there is no need to evaluate second operand - the result will be always TRUE. If you have a long chain of Short-circuit operators A || B || C || D and your first evaluates to true, then others won't be evaluated.
If you substitute Element-wise logical | to A | B | C | D then all elements will be evaluated regardless of previous operands.
| represents OR as a logical operator. || is also a logical operator called a short-circuit OR
The most important advantage of short-circuit operators is that you can use them to evaluate an expression only when certain conditions are satisfied. For example, you want to execute a function only if the function file resides on the current MATLAB path. Short-circuiting keeps the following code from generating an error when the file, myfun.m, cannot be found:
comp = (exist('myfun.m') == 2) && (myfun(x) >= y)
Similarly, this statement avoids attempting to divide by zero:
x = (b ~= 0) && (a/b > 18.5)
You can also use the && and || operators in if and while statements to take advantage of their short-circuiting behavior:
if (nargin >= 3) && (ischar(varargin{3}))
I want to replace the value of the fields in a structure array. For example, I want to replace all 1's with 3's in the following construction.
a(1).b = 1;
a(2).b = 2;
a(3).b = 1;
a([a.b] == 1).b = 3; % This doesn't work and spits out:
% "Insufficient outputs from right hand side to satisfy comma separated
% list expansion on left hand side. Missing [] are the most likely cause."
Is there an easy syntax for this? I want to avoid ugly for loops for such simple operation.
Credits go to #Slayton, but you actually can do the same thing for assigning values too, using deal:
[a([a.b]==1).b]=deal(3)
So breakdown:
[a.b]
retrieves all b fields of the array a and puts this comma-separated-list in an array.
a([a.b]==1)
uses logical indexing to index only the elements of a that satisfy the constraint. Subsequently the full command above assigns the value 3 to all elements of the resulting comma-separated-list according to this.
You can retrieve that the value of a field for each struct in an array using cell notation.
bVals = {a.b};
bVals = cell2mat( bVals );
AFAIK, you can't do the same thing for inserting values into an array of structs. You'll have to use a loop.
I'm trying to execute a block of code if one of several conditions is met. Again, for clarity, if any of the conditions are true, I'd like for the code to execute (chain of logical ORs).
When I type this code into Matlab:
if strmatch(T,'Sd') || strmatch(T,'SuperDev') || ...
strmatch(T,'lrnTrialVD') || strmatch(T,'lrnTrialVIS') || ...
strmatch(T,'lrnTrialTARGET') || strmatch(T,'lrnTrialAUD')
I get this error:
??? Operands to the || and && operators must be convertible to logical scalar values.
Can someone please tell me where I've gone wrong?
/blz
EDIT: I used the wrong function. strcmp is what I meant to use!
I believe this is because the return value of strmatch() is an array, not a scalar, and || might not be defined on array arguments. I don't have MATLAB in front of me (only Octave), but does [1, 2, 3] || [4, 5, 6, 7] work for you?
Additonally, it would be better to match a regular expression like (Sd|SuperDev|lrnTrial(VD|VIS|TARGET|AUD)) which is a bit more compact, more readable, and only needs to inspect the string 'T' once (not six times.)
This would look like:
octave-3.2.4.exe:10> T1 = "Sd"
T1 = Sd
octave-3.2.4.exe:11> T2 = "Lollipop"
T2 = Lollipop
octave-3.2.4.exe:12> regexp(T1,"(Sd|SuperDev|lrnTrial(VD|VIS|TARGET|AUD))" )
ans = 1
octave-3.2.4.exe:13> regexp(T2,"(Sd|SuperDev|lrnTrial(VD|VIS|TARGET|AUD))" )
ans = [](1x0)
strmatch doesn't necessarily return something which can be converted to a logical scalar value. It might, for example, return a vector of match locations which does not convert without further effort on your part. It's all there in the error message and the product documentation, it really is !
This post was triggered by following discussion on whether cell arrays are "normal arrays" and that vectorizaton does not work for cell arrays.
I wonder why following vectorization syntax is not implemented in MATLAB, what speaks against it:
>> {'hallo','matlab','world'} == 'matlab'
??? Undefined function or method 'eq' for input arguments of type 'cell'.
internally it would be equivalent to
[{'hallo'},{'matlab'},{'world'}] == {'matlab'}
because MATLAB knows when to cast, following works:
[{'hallo','matlab'},'world']
Cell array is an array of pointers. If both left and right side point to equal objects, isequal('hallo','hallo') returns as expected true, then why MATLAB still does not allow topmost example?
I know I can use strmatch or cellfun.
SUMMARY:
operator == which is required for vectorization in above example is eq and not isequal (other operators are < which is lt, etc.)
eq is built-in for numeric types, for all other types (like strings) MATLAB gives as freedom to overload this (and other) operators.
operator vectorization is thus well possible with cell arrays of defined type (like string) but not by default for any type.
function vectorization like myFun( myString ) or myFun( myCellOfStrings ), is also possible, you have just to implement it internally in myFun. Functions sin(val) and sin(array) work also not by witchcraft but because both cases are implemented internally.
Firstly, == is not the same as isequal. The function that gets called when you use == is eq, and the scope of each of those is different.
For e.g., in eq(A,B), if B is a scalar, the function checks each element of A for equality with B and returns a logical vector.
eq([2,5,4,2],2)
ans =
1 0 0 1
However, isequal(A,B) checks if A is identically equal to B in all aspects. In other words, MATLAB cannot tell the difference between A and B. Doing this for the above example:
isequal([2,5,4,2],2)
ans =
0
I think what you really intended to ask in the question, but didn't, is:
"Why is == not defined for cell arrays?"
Well, a simple reason is: Cells were not intended for such use. You can easily see how implementing such a function for cells can quickly get complicated when you start considering individual cases. For example, consider
{2,5,{4,2}}==2
What would you expect the answer to be? A reasonable guess would be
ans = {1,0,0}
which is fair. But let's say, I disagree. Now I'd like the equality operation to walk down nested cells and return
ans = {1,0,{0,1}}
Can you disagree with this interpretation? Perhaps not. It's equally valid, and in some cases that's the behavior you want.
This was just a simple example. Now add to this a mixture of nested cells, different types, etc. within the cell and think about handling each of those corner cases. It quickly becomes a nightmare for the developers to implement such a functionality that can be satisfactorily used by everyone.
So the solution is to overload the function, implementing only the specific functionality that you desire, for use in your application. MATLAB provides a way to do that too, by creating an #cell directory and defining an eq.m for use with cells the way you want it. Ramashalanka has demonstrated this in his answer.
There are many things that would seem natural for MATLAB to do that they have chosen not to. Perhaps they don't want to consider many special cases (see below). You can do it yourself by overloading. If you make a directory #cell and put the following in a new function eq.m:
function c = eq(a,b)
if iscell(b) && ~iscell(a)
c = eq(b,a);
else
c = cell(size(a));
for n = 1:numel(c)
if iscell(a) && iscell(b)
c{n} = isequal(a{n},b{n});
else
c{n} = isequal(a{n},b);
end
end
end
Then you can do, e.g.:
>> {'hallo','matlab','world'} == 'matlab'
ans = [0] [1] [0]
>> {'hallo','matlab','world'} == {'a','matlab','b'}
ans = [0] [1] [0]
>> {'hallo','matlab','world'} == {'a','dd','matlab'}
ans = [0] [0] [0]
>> { 1, 2, 3 } == 2
ans = [0] [1] [0]
But, even though I considered a couple of cases in my simple function, there are lots of things I didn't consider (checking cells are the same size, checking a multi-element cell against a singleton etc etc).
I used isequal even though it's called with eq (i.e. ==) since it handles {'hallo','matlab','world'} == 'matlab' better, but really I should consider more cases.
(EDIT: I made the function slightly shorter, but less efficient)
This is not unique to strings. Even the following does not work:
{ 1, 2, 3 } == 2
Cell arrays are not the same as "normal" arrays: they offer different syntax, different semantics, different capabilities, and are implemented differently (an extra layer of indirection).
Consider if == on cell arrays were defined in terms of isequal on an element-by-element basis. So, the above example would be no problem. But what about this?
{ [1 0 1], [1 1 0] } == 1
The resulting behaviour wouldn't be terribly useful in most circumstances. And what about this?
1 == { 1, 2, 3 }
And how would you define this? (I can think of at least three different interpretations.)
{ 1, 2, 3 } == { 4, 5, 6 }
And what about this?
{ 1, 2, 3 } == { { 4, 5, 6 } }
Or this?
{ 1, 2, 3 } == { 4; 5; 6 }
Or this?
{ 1, 2, 3 } == { 4, 5 }
You could add all sorts of special-case handling, but that makes the language less consistent, more complex, and less predictable.
The reason for this problem is: cell arrays can store different types of variables in different cells. Thus the operator == can't be defined well for the entire array. It is even possible for a cell to contain another cell, further exacerbating the problem.
Think of {4,'4',4.0,{4,4,4}} == '4'. what should be the result? Each type evaluates in different way.