Azzera filtri
Azzera filtri

Help determining values of a vector

2 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Walter Roberson
Walter Roberson il 25 Giu 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 Commento
Julian Epps
Julian Epps il 26 Giu 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?

Accedi per commentare.

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by