Filter löschen
Filter löschen

Help determining values of a vector

2 Ansichten (letzte 30 Tage)
Julian Epps
Julian Epps am 25 Jun. 2016
Kommentiert: Julian Epps am 26 Jun. 2016
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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 Kommentar
Julian Epps
Julian Epps am 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?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by