Your code is buggy, and that concept will fail for 3D arrays. Try this instead:
OR
Lets look at the bug in your code. First note that the LHS will always be true, as no value is zero:
So the LHS is always equal to 1, which you then compare against the size of v:
>> v = 1:4;
>> all([1,3])==size(v)
ans =
1 0
>> 1==size(v)
ans =
1 0
this will never be all true (which if requires if the condition is non-scalar), unless v is a scalar: >> v = 4;
>> all([1,3])==size(v)
ans =
1 1
>> 1==size(v)
ans =
1 1
The error was putting the parenthesis in the wrong place, it should have been right at the end:
but note that this will still fail for ND arrays. The code at the top of this answer will work for any size array.
Best Answer