Why is element of matrix not numeric but value of element is?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.....
0 comentarios
Respuestas (1)
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
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.
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!