suppressing output variables in matlab

前端 未结 2 1519
春和景丽
春和景丽 2020-12-12 07:50

I am using a function with multiple outputs in Matlab, but am only interested in one of the outputs. I would like to suppress the other output variables (i.e. avoid them bei

相关标签:
2条回答
  • 2020-12-12 08:33

    Replace any output variables you don't want with a ~ character.

    E.g.

    [~,I] = max(matrix);
    

    This pattern has an advantage over clear in that the MATLAB interpreter and just-in-time compiler can avoid the memory and CPU costs of calculating ignored variables.

    Edit

    Here is the documentation and a blog post by Loren Shure on this use of ~. I can't find any definite information about use of ignored variables for eliminating unnecessary computation.

    0 讨论(0)
  • 2020-12-12 08:38

    Use the tilde:

    [~, output2] = max(matrixA, [], 1);
    

    I doubt there would be much memory advantage (apart from clerical stuff like allocating output variables, etc.)) since the function will run completely and allocate all that it needs to. This way, you just don't get the value, and the value of the first output variable in the scope of the max function will be garbage-collected.

    0 讨论(0)
提交回复
热议问题