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!

 채택된 답변

Walter Roberson
Walter Roberson 2011년 10월 20일

0 개 추천

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

댓글 수: 3

Sarah Berurer
Sarah Berurer 2011년 10월 20일
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?
Walter Roberson
Walter Roberson 2011년 10월 20일
Try
a = subsref(ismember(eye(3),0),struct('type',{'()'},'subs',{{2}}))
Sarah Berurer
Sarah Berurer 2011년 10월 20일
Awesome thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Jan
Jan 2011년 10월 20일

1 개 추천

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

댓글 수: 4

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

댓글을 달려면 로그인하십시오.

the cyclist
the cyclist 2011년 10월 20일

0 개 추천

That sort of indexing is not possible. See for example, the accepted answer here:

카테고리

도움말 센터 및 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