Clear Filters
Clear Filters

Can/How Do I Create a Custom Numeric Type

3 views (last 30 days)
James Mulhern
James Mulhern on 29 Nov 2017
Answered: Walter Roberson on 29 Nov 2017
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
Adam on 29 Nov 2017
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 on 29 Nov 2017
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?

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 29 Nov 2017
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().


Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!