Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would someone return a matrix in an array in a Matlab function?

I'm pretty new at Matlab, but I'm looking at a function that starts out like this:

function [res] = process(input)

Inside the function they define res, do some stuff with the fields in input, and put the results of those things in res. I'm wondering what could be the reason for using the [ ] in the function declaration instead of like

function res = process(input)

which I have seen much more often. Maybe if one of you can say "putting the result in an array is good because then you can ..." Thanks.

like image 672
mring Avatar asked Nov 17 '25 05:11

mring


2 Answers

Square brackets are only required when there are multiple outputs, i.e.

function [res,other] = process(input)

If there is a single output, square brackets are optional and won't change anything, except use a little more space on your hard drive, or a little more ink when printed on paper.

My guess is that either, the function initially produced multiple outputs that got later deleted (and the square brackets were thus forgotten), or that the author likes to put square brackets for consistency with the multi-output syntax.

like image 54
Jonas Avatar answered Nov 18 '25 20:11

Jonas


Think the situation with multiple outputs, there is no one single matrix created.
Basically thats just the proper Matlab syntax. More details here.

like image 33
eat Avatar answered Nov 18 '25 20:11

eat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!