How to check if a viable is even or odd

Oldest question in the history of humanity…

If the number is even, the remainder after the division by 2 is zero, and 1 if the number is odd, so using rem function (which actually calculates the remainder of the division between two numbers) in matlab we can check if remainder is zero or not.

Example: If my var is MyVar,

  rem(MyVar,2)=0 if MyVar is even

  rem(MyVar,2)=1 if MyVar is odd

If I want to do something if my variable is even (or odd), I can write:

   if rem(MyVar,2)


   if ∼rem(MyVar,2)

Posted in checking format | Leave a comment

How to rotate a simple plot

Some times you would need to rotate your figure to display the axis rotated 90 degrees counter clockwise. The easiest way to do this rotating the view. You can do this using the command view as follows:

view([-90 90])

If you wish to rotate in the oposite direction, just do:

view([90 90])

Using this command, the axis are also rotated, so you don’t have to worry about annoying labeling issues…

Posted in Plots | Leave a comment

How to determine if the input is a scalar real number

Some times it is interesting to know whether the input is a scalar real number, as for example if we are trying to introduce the sampling frequency of a sequence. We can use some of the following commands:

isscalar:   determine whether input is scalar (true=1 or false=0).
isnumeric:   determine whether input is a number (true=1 or false=0).
isreal:   determine whether input is a real number (true=1 or false=0).
isinf:   determine whether input is finite (true=1 or false=0).

Thus, if you want to be (absolutely) sure that your variable is a real scalar number (such a valid sampling frequency) you can write

if  isscalar(Fs) && isnumeric(Fs) && isreal(Fs) && ~isinf(Fs)
     disp(‘Sampling frequency is a valid number’)

Posted in arrays, checking format, data format | Tagged , | Leave a comment

How to check array dimensions

There are a few matlab commands to determine array size or shape:

length:  finds the number of elements along the largest dimension of an array.
ndims:  returns the number of dimensions of the array.
size:      returns the sizes of each dimension in the array.
numel:  returns the number of elements in the array.
isrow:   determine whether input is row vector (true=1 or false=0).
iscolumn: determine whether input is column vector (true=1 or false=0).
isscalar:   determine whether input is scalar (true=1 or false=0).
isvector:  determine whether input is vector (true=1 or false=0).
ismatrix:  determine whether input is a matrix (true=1 or false=0).

Next table shows some examples:

Array length ndims size numel
 2 1 2 1  2 1
[1 2 3 4] 4 2 1  4 4
[1 2; 3 4] 2 2 2  2 4
 ones(3,6) 6 2 3  6 18
zeros(2,3,100) 100 3 2  3  100 600


Array iscolumn isrow isscalar isvector
 2 1 1 1 1
[1 2 3 4] 0 1 0 1
[1 2; 3 4] 0 0 0 0
 ones(3,6) 0 0 0 0
zeros(2,3,100) 0 0 0 0


Posted in arrays | Tagged , | Leave a comment

How to determine whether input belong to a set of numbers

The easiest way is to sum up the result of the comparison between the input and the array. For example, imagine that you have an array of numbers x and you want to know if 5 is one of these numbers. You can write:

if sum(5==x)==1…

If 5 is included in x, then the result will be one. You could also use the builtin function find, wich can be used to find any coincidence in an array with more parameters.

La forma más sencilla sería sumar el resultado de la comparación entre la entrada y la serie de números. Por ejemplo, imagina que tienes una seria de números x y quieres saber si 5 es uno de esos números. Podrías escribir:

if sum(5==x)==1…

Si 5 está incluido en x el resultado de la suma será 1. También puedes utilizar la función de matlab find, que permite encontrar coincidencia en un vector con diferentes parámetros.


Posted in Sin categoría | Tagged , | Leave a comment

How to determine whether input is integer

The easiest and fastest way to check if a variable is an integer independently of its format is to compare the input with its rounded version, for example:


If we are just interested in knowing if the input is an integer matlab variable (sint or uint) we can use isainteger Matlab function.

La forma más fácil y rápida para comprobar si una variable es un número entero independientemente de su formato es comparar la entrada con su versión redondeada, por ejemplo:


Si solo estamos interesados en saber si la entrada es una variable en formato entero de matlab (sint o uint) podemos utilizar la función isainteger de Matlab.

Posted in Sin categoría | Tagged , , | Leave a comment