Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
coping with "no indexing of function calls"

Subject: coping with "no indexing of function calls"

From: kj

Date: 30 May, 2013 16:11:12

Message: 1 of 8





I'm very new to MATLAB, but AFAIK, the following syntax is
categorically disallowed:

    A = foo(B)(1);

Here foo is some function. Ditto if one replaces (1) with any
other indexing subexpression (e.g. {42:end, 'frobozz'}, etc.).

This means that my code is riddled with lines like

    A = foo(B);
    A = A(1);

    X = bar(A);
    X = X{42:end, 'frobozz'};

I find this makes the code harder to read (though, I admit, this
is extremely subjective, and probably the result of three decades
of reading and writing code in languages that don't require such
circumlocutions).

Is there some other way to cope with this restriction?

(I realize that a positive answer to this question is highly
unlikely, but I guess I'm desperate.)

thanks in advance!

kj

Subject: coping with "no indexing of function calls"

From: Steven_Lord

Date: 30 May, 2013 17:55:27

Message: 2 of 8



"kj" <no.email@please.post> wrote in message
news:ko7tn0$5n$1@reader1.panix.com...
>
> I'm very new to MATLAB, but AFAIK, the following syntax is
> categorically disallowed:
>
> A = foo(B)(1);
>
> Here foo is some function. Ditto if one replaces (1) with any
> other indexing subexpression (e.g. {42:end, 'frobozz'}, etc.).

Correct, it is disallowed. In languages that only allow functions to return
one output argument, that type of syntax may make sense. But functions in
MATLAB can return multiple outputs; if that syntax were allowed, what would
you expect this to return?

[maxValues, maxLocations] = max(magic(5))(3)

Should it return the third element of each of the regular outputs from MAX?
Should it only apply to maxValues and return all of maxLocations from the
MAX call? What about cases where the function output arguments are different
sizes?

[R, p] = chol(magic(6))(:, 2)

If you execute:

[R, p] = chol(magic(6))

In this case, R should be a 2-by-2 matrix and p should be the 1-by-1 scalar
3. [The second output, p, is ALWAYS a scalar.] So asking for the second
column of R makes sense, but asking for the second column of the scalar p
does not and would error. We could probably invent some syntax to allow
different indexing into different outputs of the function, but it would
probably be difficult to read due to its generality.

> This means that my code is riddled with lines like
>
> A = foo(B);
> A = A(1);
>
> X = bar(A);
> X = X{42:end, 'frobozz'};
>
> I find this makes the code harder to read (though, I admit, this
> is extremely subjective, and probably the result of three decades
> of reading and writing code in languages that don't require such
> circumlocutions).
>
> Is there some other way to cope with this restriction?

I have a couple suggestions. If the multiline code is causing the
readability problem, you can put multiple commands on one line.

A = foo(B); A = A(1);

You could write a function that accepts the temporary output from the
function and the indices and extract just those elements of the temporary
output.

extractCertainElementsFromOneOutputFunction = @(tmp, index) tmp(index);
A = extractCertainElementsFromOneOutputFunction(foo(B), 1)

You could modify foo and bar to accept inputs that indicate what elements of
the output should be returned and return just those outputs, somewhat like
SIZE does.

M = ones(4, 5, 6);
sz = size(M) % size in all the non-trailing-singleton dimensions
sz2_twostep = sz(2)
sz2_onestep = size(M, 2) % size in the 2nd dimension only

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: coping with "no indexing of function calls"

From: dpb

Date: 30 May, 2013 18:04:04

Message: 3 of 8

On 5/30/2013 11:11 AM, kj wrote:
...

> X = bar(A);
> X = X{42:end, 'frobozz'};
>
> I find this makes the code harder to read (though, I admit, this
> is extremely subjective, and probably the result of three decades
> of reading and writing code in languages that don't require such
> circumlocutions).
>
...

I guess I'm wondering what the latter above is supposed to represent
firstly and secondly what are these "languages that don't require such
circumlocutions" of which you speaketh and what would be their syntax?

I grok Steven's response on why on functions altho some of the Octave
multiple indexing facilities would surely be a nice addition to ML...

--

Subject: coping with "no indexing of function calls"

From: kj

Date: 30 May, 2013 20:00:16

Message: 4 of 8

In <ko83qf$4fo$1@newscl01ah.mathworks.com> "Steven_Lord" <slord@mathworks.com> writes:

>...In languages that only allow functions to return
>one output argument, that type of syntax may make sense. But functions in
>MATLAB can return multiple outputs...

Thanks, that helps.

The only language I know that has anything resembling this situation
(not quite) is Perl's context dependence (scalar vs list vs boolean
contexts). In Perl one can force a scalar context with the `scalar` keyword; e.g.:

    $x = (scalar foo($bar))->[3];

FWIW. My point is that, to a naive observer like me, it doesn't
seem that it would be all that difficult to devise a fairly minimal
extension to the syntax that would eliminate the ambiguity you
pointed out. (But, of course, I won't hold my breath.)

>You could write a function that accepts the temporary output from the
>function and the indices and extract just those elements of the temporary
>output.

>extractCertainElementsFromOneOutputFunction = @(tmp, index) tmp(index);
>A = extractCertainElementsFromOneOutputFunction(foo(B), 1)

I really like this idea. In fact, I don't see why it has to be a
temporary function.

kj

Subject: coping with "no indexing of function calls"

From: kj

Date: 30 May, 2013 20:09:25

Message: 5 of 8

In <ko84ah$fh8$1@speranza.aioe.org> dpb <none@non.net> writes:

>On 5/30/2013 11:11 AM, kj wrote:
>...

>> X = bar(A);
>> X = X{42:end, 'frobozz'};
>>
>> I find this makes the code harder to read (though, I admit, this
>> is extremely subjective, and probably the result of three decades
>> of reading and writing code in languages that don't require such
>> circumlocutions).
>>
>...

>I guess I'm wondering what the latter above is supposed to represent
>firstly and secondly what are these "languages that don't require such
>circumlocutions" of which you speaketh and what would be their syntax?

I'm sorry that my writing offends you. English is not my first
language.

Subject: coping with "no indexing of function calls"

From: dpb

Date: 30 May, 2013 20:14:15

Message: 6 of 8

On 5/30/2013 3:09 PM, kj wrote:
> In<ko84ah$fh8$1@speranza.aioe.org> dpb<none@non.net> writes:
>
>> On 5/30/2013 11:11 AM, kj wrote:
>> ...
>
>>> X = bar(A);
>>> X = X{42:end, 'frobozz'};
>>>
>>> I find this makes the code harder to read (though, I admit, this
>>> is extremely subjective, and probably the result of three decades
>>> of reading and writing code in languages that don't require such
>>> circumlocutions).
>>>
>> ...
>
>> I guess I'm wondering what the latter above is supposed to represent
>> firstly and secondly what are these "languages that don't require such
>> circumlocutions" of which you speaketh and what would be their syntax?
>
> I'm sorry that my writing offends you. English is not my first
> language.

Not that at all; I was just trying to understand what your proposed
syntax above was intended to mean first and then I am not aware of a
language that has such a syntax as you propose so was curious as to what
languages you were speaking of and how they would write what it is that
the above is to represent.

Sorry that I tried to be a little cutesy in my wording of the
question--it was intended purely in a sense of levity (i.e., jokingly)
not as criticism nor any such intent.

--

Subject: coping with "no indexing of function calls"

From: Steven_Lord

Date: 31 May, 2013 13:55:07

Message: 7 of 8



"kj" <no.email@please.post> wrote in message
news:ko8b4g$ojc$1@reader1.panix.com...
> In <ko83qf$4fo$1@newscl01ah.mathworks.com> "Steven_Lord"
> <slord@mathworks.com> writes:

*snip*

>>You could write a function that accepts the temporary output from the
>>function and the indices and extract just those elements of the temporary
>>output.
>
>>extractCertainElementsFromOneOutputFunction = @(tmp, index) tmp(index);
>>A = extractCertainElementsFromOneOutputFunction(foo(B), 1)
>
> I really like this idea. In fact, I don't see why it has to be a
> temporary function.

It doesn't have to be. I wrote it as an anonymous function because it was
less typing than defining a "named" function, although the fact that I used
a 43 character variable name probably negated that advantage ;)

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: coping with "no indexing of function calls"

From: Eric Sampson

Date: 5 Jun, 2013 23:13:06

Message: 8 of 8

"Steven_Lord" <slord@mathworks.com> wrote in message <ko83qf$4fo$1@newscl01ah.mathworks.com>...
>
>
> "kj" <no.email@please.post> wrote in message
> news:ko7tn0$5n$1@reader1.panix.com...
> >
> > I'm very new to MATLAB, but AFAIK, the following syntax is
> > categorically disallowed:
> >
> > A = foo(B)(1);
> >
> > Here foo is some function. Ditto if one replaces (1) with any
> > other indexing subexpression (e.g. {42:end, 'frobozz'}, etc.).
>
> Correct, it is disallowed. In languages that only allow functions to return
> one output argument, that type of syntax may make sense. But functions in
> MATLAB can return multiple outputs; if that syntax were allowed, what would
> you expect this to return?
>
> [maxValues, maxLocations] = max(magic(5))(3)
>
> Should it return the third element of each of the regular outputs from MAX?
> Should it only apply to maxValues and return all of maxLocations from the
> MAX call? What about cases where the function output arguments are different
> sizes?
>
> [R, p] = chol(magic(6))(:, 2)
>
> If you execute:
>
> [R, p] = chol(magic(6))
>
> In this case, R should be a 2-by-2 matrix and p should be the 1-by-1 scalar
> 3. [The second output, p, is ALWAYS a scalar.] So asking for the second
> column of R makes sense, but asking for the second column of the scalar p
> does not and would error. We could probably invent some syntax to allow
> different indexing into different outputs of the function, but it would
> probably be difficult to read due to its generality.
>
(snip)
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Steve, it seems to me that the most handy/requested use case would be to allow indexing of single-output functions, and then just error out if an index is supplied and multiple outputs are requested. Would that be possible? It would probably satisfy the majority of people's desire for this type of functionality - as you say, adding new syntax to indicate indexing into multiple outputs would probably be confusing. Of course I'd think it would still be desirable to be able to do something like this: [~, maxLocations] = max(magic(5))(3)

Cheers

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us