Create and indexing a matrix simultaneously

Hello,
I'm trying to create an index a matrix in one line. There are a lot of examples I could use, but if I want the second element I would like to write something like:
a=ismember(eye(3),0)(2)
or even just
a=eye(3)(2)
The error I always run in to is this:
??? Error: ()-indexing must appear last in an index expression.
Any help is greatly appreciated!

 采纳的回答

a = subsref(ismember(eye(3),0),struct('type','()','subs',2))

3 个评论

Walter, this is excellent, exactly the kind of thing I'm looking for. I've been playing around with this for a while, but I keep getting the error "??? Error using ==> subsref
The "subs" field for the subscript argument to SUBSREF and SUBSASGN must be a cell or character array." Any ideas how to fix this?
Try
a = subsref(ismember(eye(3),0),struct('type',{'()'},'subs',{{2}}))

请先登录,再进行评论。

更多回答(3 个)

An anchor drifts along
Green and red circling endlessly
A fallen MATLAB feature

4 个评论

Daniel & Cyclist: You have been faster! But I have the direct link to Walters suggestion ;-)
and now I do too :)
The power of editing.
I have the editor power - I can even *remove* the anchor from your answer again! (Is there an emoticon for a sardonic grinning?)
Nice! I actually wanted to delete this useless answer. But suddenly it became art. The power will be with us.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by