Clear Filters
Clear Filters

Help determining values of a vector

2 views (last 30 days)
Hello, I am writing a code that asks the user to input a number a values then takes those values and puts them into an array. Then evaluates each element of the array to determine if it is pos, neg, even, or odd. Here's what I have
function [Poseven]= Vect(UserNum);
str = inputdlg('Enter 10 numbers seperated by spaces or commas');
UserNum = str2num(str{1});
if mod(UserNum, 2) == 0, UserNum > 0;
Poseven = UserNum(1,1:end)
end
end
The only thing I am not understanding is why or how rather to display the numbers of the original vector where the modulus value is zero and greater than zero.

Accepted Answer

Walter Roberson
Walter Roberson on 25 Jun 2016
Your lines
if mod(UserNum, 2) == 0, UserNum > 0;
Poseven = UserNum(1,1:end)
end
are the same as
if mod(UserNum, 2) == 0
UserNum > 0;
Poseven = UserNum(1,1:end)
end
which calculates whether UserNum > 0 and then throws away the result of the comparison. If you want to combine conditions, you need to use one of &&, ||, &, or |
Note: you probably want to use either a loop or logical indexing.
  1 Comment
Julian Epps
Julian Epps on 26 Jun 2016
Oh okay so this will check the two conditions instead of overwriting one with the other, Thanks! The other problem I am facing is that I need to return only the positive even numbers of of the original UserNum. the lines :
if mod(UserNum, 2) == 0 & (UserNum > 0);
Poseven = UserNum(1,1:end)
Don't seem to be returning those original values but I am not getting an error when I run it, any help or explanation as to why?

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!