Thread Subject: indexing of a function's return

Subject: indexing of a function's return

From: Norman

Date: 10 Sep, 2008 11:16:02

Message: 1 of 9

Hi!

I don't know if the subject is clear enough. But this explanation will certainly do:

Many functions (e.g. cov() ) return a vector or even a matrix of values. How can I immediately access one (or more) elements of this?

What I want is something like

a = cov(A,B)(1,3)

meaning to extract the (1,3)-element of the matrix returned by cov().

Of course I can first assign the matrix to a variable and then index that one, but that's seems to be a detour (it's less readable and maybe even slower).

Any ideas?

Norman

Subject: indexing of a function's return

From: John D'Errico

Date: 10 Sep, 2008 11:26:02

Message: 2 of 9

"Norman " <norman.nospam.violet@tu-berlin.de> wrote in message <ga8a9i$jin$1@fred.mathworks.com>...
> Hi!
>
> I don't know if the subject is clear enough. But this explanation will certainly do:
>
> Many functions (e.g. cov() ) return a vector or even a matrix of values. How can I immediately access one (or more) elements of this?
>
> What I want is something like
>
> a = cov(A,B)(1,3)
>
> meaning to extract the (1,3)-element of the matrix returned by cov().
>
> Of course I can first assign the matrix to a variable and then index that one, but that's seems to be a detour (it's less readable and maybe even slower).
>
> Any ideas?

You can't do this directly, at least not in the
current release of Matlab.

Of course, you could write a function to do it,
but that would seem less readable and less
efficient too.

John

Subject: indexing of a function's return

From: Nick Clark

Date: 10 Sep, 2008 11:35:02

Message: 3 of 9

My gut reaction is that most functions would require the whole output of the function to be calculated stored in memory anyway (especially something which is recursive like convolution or IIR filtering) even if you just wanted to "cherry pick" the value(s) required, meaning the overhead for indexing after generating the output really would be minute.

You could free a bit of memory by doing . .
a = cov(A,B);
a = a(1,3);
b = 4*a blah blah


Or save a line by . . .
a = cov(A,B);
b = 4*a(1,3) blah blah

 . . . but apart from this I'm stumped. I'd be interested if anyone can think of a better way!

Subject: indexing of a function's return

From: Husam Aldahiyat

Date: 10 Sep, 2008 11:37:01

Message: 4 of 9

You can use size(a,1) or size(a,2) for the length and width of a, but that's the only case I know/

Subject: indexing of a function's return

From: Steve Amphlett

Date: 10 Sep, 2008 11:43:03

Message: 5 of 9

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <ga8asa$nv9$1@fred.mathworks.com>...
> "Norman " <norman.nospam.violet@tu-berlin.de> wrote in message <ga8a9i$jin$1@fred.mathworks.com>...
> > Hi!
> >
> > I don't know if the subject is clear enough. But this explanation will certainly do:
> >
> > Many functions (e.g. cov() ) return a vector or even a matrix of values. How can I immediately access one (or more) elements of this?
> >
> > What I want is something like
> >
> > a = cov(A,B)(1,3)
> >
> > meaning to extract the (1,3)-element of the matrix returned by cov().
> >
> > Of course I can first assign the matrix to a variable and then index that one, but that's seems to be a detour (it's less readable and maybe even slower).
> >
> > Any ideas?
>
> You can't do this directly, at least not in the
> current release of Matlab.
>
> Of course, you could write a function to do it,
> but that would seem less readable and less
> efficient too.
>
> John


You can do it with the return value from a cell array access:

>> a{1}=1:10;
>> a{1}(2:5)

ans =

     2 3 4 5

Which is broadly similar. So there may be hope for the future.

Subject: indexing of a function's return

From: Bruno Luong

Date: 10 Sep, 2008 11:48:01

Message: 6 of 9

A bandage work-around is using subsref

% Put this on subindex.m
function val = subindex(array, varargin)

subs=varargin;
subs(end+1:ndims(array))={':'};
val = subsref(array, struct('type','()', 'subs', {subs}));

end

% Few Tests

subindex(rand(3),1,3)
subindex(rand(3),1,:)
subindex(rand(3),1,3)
subindex(rand(3))

% Bruno

Subject: indexing of a function's return

From: Bruno Luong

Date: 10 Sep, 2008 12:09:02

Message: 7 of 9

Or Simply do this if you do not want automatic dimensional expansion indexing

% Put this on subindex.m
function val = subindex(array, varargin)

subs=varargin;
val = subsref(array, struct('type','()', 'subs', {subs}));

end

% Bruno
 

Subject: indexing of a function's return

From: John D'Errico

Date: 10 Sep, 2008 12:26:02

Message: 8 of 9

"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message <ga8bs7$2ka$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <ga8asa$nv9$1@fred.mathworks.com>...
> > "Norman " <norman.nospam.violet@tu-berlin.de> wrote in message <ga8a9i$jin$1@fred.mathworks.com>...
> > > Hi!
> > >
> > > I don't know if the subject is clear enough. But this explanation will certainly do:
> > >
> > > Many functions (e.g. cov() ) return a vector or even a matrix of values. How can I immediately access one (or more) elements of this?
> > >
> > > What I want is something like
> > >
> > > a = cov(A,B)(1,3)
> > >
> > > meaning to extract the (1,3)-element of the matrix returned by cov().
> > >
> > > Of course I can first assign the matrix to a variable and then index that one, but that's seems to be a detour (it's less readable and maybe even slower).
> > >
> > > Any ideas?
> >
> > You can't do this directly, at least not in the
> > current release of Matlab.
> >
> > Of course, you could write a function to do it,
> > but that would seem less readable and less
> > efficient too.
> >
> > John
>
>
> You can do it with the return value from a cell array access:
>
> >> a{1}=1:10;
> >> a{1}(2:5)
>
> ans =
>
> 2 3 4 5
>
> Which is broadly similar. So there may be hope for the future.

Oh, there is hope for the future. I recall
a comment on this NG that it has been
considered for a future version.

John

Subject: indexing of a function's return

From: Steven Lord

Date: 10 Sep, 2008 12:39:11

Message: 9 of 9


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
news:ga8dcu$htn$1@fred.mathworks.com...
> Or Simply do this if you do not want automatic dimensional expansion
> indexing
>
> % Put this on subindex.m
> function val = subindex(array, varargin)
>
> subs=varargin;
> val = subsref(array, struct('type','()', 'subs', {subs}));

This works for simple indexing, but if you wanted to expand this to more
complicated indexing, I have two suggestions.

1) The name of this function is very close to the name of the method you'd
need to overload for a class to be able to use objects of that class as
indices, SUBSINDEX. I can understand why you want to use SUBINDEX, but I'm
just saying it could cause confusion.

2) Instead of explicitly creating the struct array that you pass into
SUBSREF, you might find the SUBSTRUCT function useful, particularly if you
go to multiple levels of indexing.

--
Steve Lord
slord@mathworks.com

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
indexing Norman 10 Sep, 2008 07:20:05
return value Norman 10 Sep, 2008 07:20:05
function Norman 10 Sep, 2008 07:20:05
rssFeed for this Thread

Contact us at files@mathworks.com