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

조회 수: 5 (최근 30일)
S Crombie
S Crombie 2018년 2월 28일
댓글: Walter Roberson 2018년 3월 1일
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.....

답변 (1개)

Steven Lord
Steven Lord 2018년 2월 28일
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
S Crombie
S Crombie 2018년 3월 1일
편집: S Crombie 2018년 3월 1일
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 2018년 3월 1일
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.

댓글을 달려면 로그인하십시오.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by