Vector/martix limit size?

2 views (last 30 days)
bondpen
bondpen on 14 Aug 2018
Edited: Stephen23 on 14 Aug 2018
Hi
I not sure what I have done wrong basically the above if statement make sure the values in vector is desire number and if this above equation is correct the below is executed to make sure vector is 1x3 but if vector is same dimension it will give an output number if not it should go else. I am pretty sure above equation is correct but is this right way or is there another method?
elseif all([1,3]) == size(v)
answer = 1;
else
answer = 0;
  6 Comments
Stephen23
Stephen23 on 14 Aug 2018
Edited: Stephen23 on 14 Aug 2018
@David Wu: if you want to test if the input matrix is zero or ones then an explicit test would be clearer:
all(ismember(v,[0,1]))
"Sorry I need import matrix to be zero or one."
Your explanations and your code contradict each other: you write that you want the ALL (?) of the input values to be zero or one, but in this code
(v(1,3))^v(1,3) == 1 || (v(1,1))^v(1,1) == 1
you use OR || operator, which means only ONE of them must be zero or one. So which is it that you want: ALL of them, or just one (ANY) of them?
>> v = [1,0,1];
>> all(ismember(v,[0,1])) % ALL
ans = 1
>> any(ismember(v,[0,1])) % ANY
ans = 1
>> v = [1,2,3];
>> all(ismember(v,[0,1])) % ALL
ans = 0
>> any(ismember(v,[0,1])) % ANY
ans = 1
Which do you want?
In any case, both of these are trivial to achieve using ismember, or with the code I showed you in my last comment. Did you try it? I just simplified your code down to this all((v.^v)==1). It is easy to test, it gives the same output as ismember code above:
>> v = [1,0,1];
>> all((v.^v)==1) % ALL
ans = 1
>> any((v.^v)==1) % ANY
ans = 1
>> v = [1,2,3];
>> all((v.^v)==1) % ALL
ans = 0
>> any((v.^v)==1) % ANY
ans = 1
Stephen23
Stephen23 on 14 Aug 2018
Edited: Stephen23 on 14 Aug 2018
@David Wu: do NOT call your function input, as this shadows the inbuilt input function (although to be honest, getting rid of input would improve many beginners' code quite a lot...)
"if it meet the if statement it will go down to elseif "
It is not clear what you mean: in MATLAB if the a condition is true then it will not "go down" to the next condition (e.g. elseif).
Your code is buggy. Consider what would happen if the input v is scalar: you try to access v(1,3), which would then throw an error. For some reason you decided to check the size after checking its values, at which point the size check is basically useless.
Anyway, here is a simpler version, assuming that your aim is to check if you have a row vector of three elements, containing only ones and zeros:
myfun = @(v) isequal(size(v),[1,3]) && all(ismember(v,0:1));
Try it and see what it does. It does not fail for scalars like your code, and it is a lot simpler.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 14 Aug 2018
Edited: Stephen23 on 14 Aug 2018
Your code is buggy, and that concept will fail for 3D arrays. Try this instead:
isrow(v) && numel(v)==3
OR
isequal(size(v),[1,3])
Lets look at the bug in your code. First note that the LHS will always be true, as no value is zero:
>> all([1,3])
ans = 1
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) % exactly equivalent
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) % exactly equivalent
ans =
1 1
The error was putting the parenthesis in the wrong place, it should have been right at the end:
all([1,3]==size(v))
but note that this will still fail for ND arrays. The code at the top of this answer will work for any size array.

More Answers (0)

Categories

Find more on Data Types in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!