Why is element of matrix not numeric but value of element is?

4 visualizaciones (últimos 30 días)
S Crombie
S Crombie el 28 de Feb. de 2018
Comentada: Walter Roberson el 1 de Mzo. de 2018
Can someone help me explain this? I am testing an element of a matrix to see if it's numeric (the matrix also contains some symbolic variables).
>> T12test2(1)
ans =
4967757600021511/81129638414606681695789005144064
>> isnumeric(T12test2(1))
ans =
logical
0
>> isnumeric(4967757600021511/81129638414606681695789005144064)
ans =
logical
1
When I test the element of the matrix it is not numeric, but when I test the value of the element it is numeric. Puzzled.....

Respuestas (1)

Steven Lord
Steven Lord el 28 de Feb. de 2018
Whether an array is numeric or not is a function of the class of the array, not the contents of the array.
The array T12test2 is of class sym so T12test2(1) is also of class sym, and sym objects are not numeric. They can contain numbers, but they are not themselves numeric.
syms x
z = sym(5);
isnumeric(x) % false
isnumeric(z) % false
When you divide two numbers like you did in your second isnumeric call, you receive a number that is stored as an array of class double. A double array contains numbers and is also numeric.
isnumeric(42) % true
isnumeric(1/2) % true
y = pi; isnumeric(y) % true
  4 comentarios
S Crombie
S Crombie el 1 de Mzo. de 2018
Editada: S Crombie el 1 de Mzo. de 2018
My matrix is a homogeneous rotation matrix generated from a function which takes four inputs (they are the Denavit-Hartenberg parameters for a robot joint to be exact, two are distances and two are angles in radians).
When I run the function with a mixture of numeric and symbolic inputs some of the output matrix elements appear as huge fractions, although these evaluate to zero effectively. For example the fraction in my original post above. The other matrix elements are numbers, or expressions which can be evaluated, eg 2^(1/2)/2 and others which can't eg d2 (which is a symbolic variable).
I want to go through the matrix, evaluating the elements, set any that are effectively zero to exactly zero to make the matrix easier to read, but leave other elements and any symbolic variables unchanged. My efforts so far have failed because any operation I can apply to the matrix elements which are numbers throws an error when applied to a matrix element which is a symbolic variable.
So ideally I want some way of distinguishing the symbolic variables in my matrix.
Walter Roberson
Walter Roberson el 1 de Mzo. de 2018
Determining whether a particular element of a matrix contains at least one symbolic variable is easy:
arrayfun( @(X) isempty(symvar(X)), YourMatrix )
However, you undoubtedly have a matrix that contains expressions in which you want to zero out the coefficients involved with some terms but not others. That is something that is difficult to impossible to do at the MATLAB level, but is possible to do having the MATLAB level telling the symbolic engine to run some code.

Iniciar sesión para comentar.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by