Can/How Do I Create a Custom Numeric Type

조회 수: 4 (최근 30일)
James Mulhern
James Mulhern 2017년 11월 29일
답변: Walter Roberson 2017년 11월 29일
I have an array that I am using to store the output data for my project. I initialize it using NaN and then overwrite the data as I calculate different parameters, however in some columns not every row is overwritten because some things are not calculated. I would like to create a custom numeric type that I can use to initialize the array instead of using NaN. NaN works, but it indicated that the cell had a calculation performed. I would like to create an numeric type that is like "Blank" or "No-Calc" or something, this way it is clear to an end user that that cell was not calculated.
Is this possible to do?
I think it could be done by making it a cell array, however it would become tedious to be constantly converting to and from a cell.
This is essentially what my data looks like
[1, 2, 3]
[1, 2, NaN]
[1, 2, 3]
This is what I want
[1, 2, 3]
[1, 2, Blank]
[1, 2, 3]
I also do not want to use 0 because it the cells could actually be equal to zero. Maybe Inf or -Inf would be better. Anyway please let me know if I can create a custom numeric type, and if so how to do it. Thanks
  댓글 수: 2
Adam
Adam 2017년 11월 29일
You can inherit a class from e.g. double or int16 or any numeric type and overload functions on it, but it isn't for the faint-hearted!
It isn't something I have ever done though I am, at present, working on ideas for a similar problem - dealing with sparse arrays where I want 0 to actually be 0 rather than representing just an element of the array that is not filled. That is proving complicated to do in a generic format though so I am creating a more specific wrapper class for a sparse array for my immediate need.
I would suggest you consider something similar if you really want to go down this route - don't try to create a generic type, create one that solves the actual problem you want solving and doesn't need to be generically interchangeable with other numeric arrays in every situation.
And yes, you could do this in a cell array, but cell arrays are horrid for numeric-only usage.
Rik
Rik 2017년 11월 29일
So your main objection is that it isn't clear to an end-user what NaN means in this context? Because then this seems like killing mosquitos with nukes. Isn't it easier to explain what NaN means in comments or in a GUI?

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

답변 (1개)

Walter Roberson
Walter Roberson 2017년 11월 29일
I suspect that all you need to override is the display routine -- though I would also be concerned about what happened with fprintf() and sprintf().

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by