How would I do this with a while loop script?

14 次查看(过去 30 天)
I allow the user to input their own vector. Then my script shows the vector as a decimal number but with the integers as zeros. I now want my if statement showing if the vector has one or more numbers that are not integers as an incorrect input. I know how to get the script to display "incorrect input" but how would I get the part showing if the vector has one or more numbers that are not integers?

采纳的回答

Adam Danz
Adam Danz 2018-9-16
编辑:Adam Danz 2018-9-16
Use isinteger() which returns 0/1 for each element of the vector.
Then use any() to detect if there are any integers.
any(isinteger(vec))
Note that this does not and should not be done in a loop.
  5 个评论
Adam Danz
Adam Danz 2018-9-16
Now it's really unclear what you're trying to do. You're asking the user to enter integers and then you're trying to return the decimals to their input?
If I ignore all of that, this is how you can return a message to the command window and the results.
vec=input('Please enter a vector consisting of integers:');
vec(vec<0)=0;
fprintf('The decimal number: \n')
disp(vec)
Note that the user will have to enter a vector using square brackets
[-1 -pi, 99, 42, -9.83]
or using colon operator
-5.5: 0.15 : 10
Adam Danz
Adam Danz 2018-9-16
编辑:Adam Danz 2018-9-16
I have to write a script which I believe has to be a while loop since I have to ask the user if they want to repeat with "Enter 1 if you want to repeat".
One method would be to use a while loop. An alternative would be to write a nested function that prompts the user when called.
Second, the script should return the decimal number that is formed by replacing all negative numbers by zero such as [2 -3 4 -5] to 2040.
So, just to be clear from your example, 1) the user is expected to enter integers, 2) the integers will be concatenated for form 1 integer (negatives converted to 0), and 3) the decimal form will be returned which will always be 0?
Third, if the vector has one or more integers then it should result in an incorrect input.
Which, according to your example, will always be the case. Maybe there's a better example that will help.

请先登录,再进行评论。

更多回答(0 个)

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by