Error-checking for integers, MATLAB in Mathematics

Error-Checking for Integers:

As MATLAB uses the type double by default for all the values, to check to make confirm that the user has entered an integer, the program have to convert the input value to an integer type (e.g., int32) and then check to see whether it is equivalent to the original input. The following illustrations illustrate the concept.

When the value of the variable num is a real number, converting it to the type int32 will round it; therefore the result is not similar as the original value.

 

>> num = 3.3;

>> inum = int32(num)

inum =

3

>> num == inum

ans =

0

 

If, on another hand, the value of the variable num is an integer, convert it to an integer type will not change the value.

>> num = 4;

>> inum = int32(num)

inum =

4

>> num == inum

ans =

1

The script below uses this idea to error-check for integer data; it loops until the user properly enters an integer.

741_Error-Checking for Integers.png

Posted Date: 10/19/2012 6:15:24 AM | Location : United States







Related Discussions:- Error-checking for integers, Assignment Help, Ask Question on Error-checking for integers, Get Answer, Expert's Help, Error-checking for integers Discussions

Write discussion on Error-checking for integers
Your posts are moderated
Related Questions
Changing Dimensions: In addition to transpose operator, the MATLAB has some built-in functions which change the dimensions or configuration of matrices, involving fliplr, resh

Example of Recursive functions: This definition is recursive as a factorial is defined in terms of the other factorial. There are two parts to any recursive definition: the co

Plot Functions: Faraway, we have plotted to generate two-dimensional plots and bar to generate bar charts. We have seen how to clear the Figure Window by using clf, and how to

5 p2+8p+15, 3 p2-3p-18, 12 p2-p-30

Error-checking for integers: illustrations of running this script as shown below: >> readoneint Enter an integer: 9.5 Invalid! Enter an integer: 3.6 Invalid

Write a MATLAB function [d1, u1, l1, c1, r1] = NaiveGaussArrow(d, u, l, c, r) that takes as input the 5 vectors de ned above representing A. This function performs Naive Gauss redu

Built in recursive function in MATLAB: We have seen that the built-in function in MATLAB to compute factorials, termed as the factorial and we know how to implement the iterat

Writing variables to a File: The save command is used to write variables to a file, or to append the variables to a MAT-file. By the default, save function writes to a MAT-fil

Refer the subset of a matrix: It is also possible to refer to the subset of a matrix. For illustration, this refers to the first & second rows, second & third columns: >> m

Script a MATLAB program which meets the following speci cations: The program expects an input of a two-variable real-valued continuous function f : R^2--> R The program is to cal