Help determining values of a vector

1 次查看(过去 30 天)
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.

采纳的回答

Walter Roberson
Walter Roberson 2016-6-25
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 个评论
Julian Epps
Julian Epps 2016-6-26
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?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by