MATLAB Answers

Equivalent of c++'s NULL or python' s None in MATLAB

145 ビュー (過去 30 日間)
Fawad Khan
Fawad Khan 2021 年 5 月 20 日
回答済み: Bruno Luong 2021 年 5 月 21 日
I have a c++ code where certain variables (as well as certain user defined class objects) are set to NULL under some conditions. I'm translating this code to MATLAB. Should I assign those variables/class objects as [] (and use isempty to check conditions) or should I assign them NaN (and use isnan to check conditions)? What would be more correct and efficient? Or do i assign them to logical operator "false"?


Walter Roberson
Walter Roberson 2021 年 5 月 20 日
C / C++ NULL takes up space, as does python None . If you need something that takes up space, then using [] would not be appropriate.
C / C++ NULL is a valid member of a pointer class. You can store it in a single array of pointer type -- and it often is stored that way. For example if you create a common two-level array, like int** for a pointer to an array of pointers to int, then it would be common to set the unused slots to NULL, and there are no class problems in doing that.
python None is a different class. If you have an object which is expected to be uniform data type, then it cannot have None stored in it.
MATLAB's NaN exists in single precision and double precision, and those are full members of those classes - the IEEE 754 standard defines specific bit patterns for NaN, just like it defines specific bit patterns for infinity. So NaN is suitable for use in a homogenous single or double array situation, and would still be able to test for special value within that class. Contrawise, NaN cannot be used in a homogenous uint8 array or any other class other than single or double.
logical values are not any numeric class, but they silently convert to numeric in many situations. So [1, 2, false, 4] would work, but it would convert to [1, 2, 0, 4] and you would then lose being able to test each entry for special value.
If your purpose is to signal "no value" in a context where it is expected that the results will be assigned to a variable, then using [] would be more MATLAB-y. If you need the value to take up space, then use NaN if regular values would be single or double.
In some cases it makes the most sense to return a logical vector of success / failure. An example of this is ismember(), where the first output is success/failure and the second output is the index location.
  1 件のコメント
Fawad Khan
Fawad Khan 2021 年 5 月 21 日
thank you for the suggestions.


その他の回答 (3 件)

James Tursa
James Tursa 2021 年 5 月 20 日
編集済み: James Tursa 2021 年 5 月 20 日
This will probably depend on how these are used downstream in your code and whether you have arrays of them to deal with. If they are scalar variables, then probably either [] or nan will work. If they are only being used for flags, then logical true/false would work and is probably the fastest. If there are arrays of them, then you are probably stuck with nan or logical. If you are processing arrays of them downstream in your code beyond just flag checking, many functions can automatically deal with and/or ignore the nan, so again nan might be your best choice.
Bottom line is I wouldn't worry so much about the efficiency of isempty( ) vs isnan( ) vs logical checking. I would pay more attention to how they will be used downstream in your code. Hard to advise any more than this without knowing more about what the code is doing.
  1 件のコメント
Fawad Khan
Fawad Khan 2021 年 5 月 21 日
thank you.


Steven Lord
Steven Lord 2021 年 5 月 20 日
In addition to what James and Walter have written, if you're writing your own class and want to be able to indicate that some element of an array of instances of your class are "missing" you could implement your class to satisfy the missing contract. If you do I recommend you write a test that it correctly satisfies the contract using the matlab.test.behavior.Missing class as the base class for that test.
  1 件のコメント
Fawad Khan
Fawad Khan 2021 年 5 月 21 日
I'll try this. Thank you.


Bruno Luong
Bruno Luong 2021 年 5 月 21 日
If you use recent MATLAB you can use also missing and ismissing


Community Treasure Hunt

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

Start Hunting!

Translated by