From: Pinpress on
Hi,

I need to choose the first element of a vector that's modified by a conditional statement. For example, suppose:

I = [1 2 3 4];
R = logical( [0 1 0 1] );

Then I(R) returns [2 4]. What I need to do is to find "2" and assign it to a new variable, which has to be a one element. I can do the following:

t = I(R); t = t(1);

But this is not concise enough. I just hope there is a better way to do this. For example, something like in C, "I(R)[1]", sort of things. However, this syntax does not work in Matlab. Thanks!
From: us on
"Pinpress" <nospam__(a)yahoo.com> wrote in message <hte45u$rqt$1(a)fred.mathworks.com>...
> Hi,
>
> I need to choose the first element of a vector that's modified by a conditional statement. For example, suppose:
>
> I = [1 2 3 4];
> R = logical( [0 1 0 1] );
>
> Then I(R) returns [2 4]. What I need to do is to find "2" and assign it to a new variable, which has to be a one element. I can do the following:
>
> t = I(R); t = t(1);
>
> But this is not concise enough. I just hope there is a better way to do this. For example, something like in C, "I(R)[1]", sort of things. However, this syntax does not work in Matlab. Thanks!

NO... as has been discussed over and over, again, in this NG, contractions don't work in ML...

one of the solutions

d=1:4;
dl=logical([0,0,1,1]);
r=d(find(dl,1,'first')) % <- a horrible construct...
% r = 3

us
From: Pinpress on
Thanks us! Your replies are always good ... it is concise enough for me.

"us " <us(a)neurol.unizh.ch> wrote in message <hte4ms$42h$1(a)fred.mathworks.com>...
> "Pinpress" <nospam__(a)yahoo.com> wrote in message <hte45u$rqt$1(a)fred.mathworks.com>...
> > Hi,
> >
> > I need to choose the first element of a vector that's modified by a conditional statement. For example, suppose:
> >
> > I = [1 2 3 4];
> > R = logical( [0 1 0 1] );
> >
> > Then I(R) returns [2 4]. What I need to do is to find "2" and assign it to a new variable, which has to be a one element. I can do the following:
> >
> > t = I(R); t = t(1);
> >
> > But this is not concise enough. I just hope there is a better way to do this. For example, something like in C, "I(R)[1]", sort of things. However, this syntax does not work in Matlab. Thanks!
>
> NO... as has been discussed over and over, again, in this NG, contractions don't work in ML...
>
> one of the solutions
>
> d=1:4;
> dl=logical([0,0,1,1]);
> r=d(find(dl,1,'first')) % <- a horrible construct...
> % r = 3
>
> us
From: Steven Lord on

"Pinpress" <nospam__(a)yahoo.com> wrote in message
news:hte45u$rqt$1(a)fred.mathworks.com...
> Hi,
>
> I need to choose the first element of a vector that's modified by a
> conditional statement. For example, suppose:
>
> I = [1 2 3 4];
> R = logical( [0 1 0 1] );
>
> Then I(R) returns [2 4]. What I need to do is to find "2" and assign it to
> a new variable, which has to be a one element. I can do the following:
>
> t = I(R); t = t(1);
>
> But this is not concise enough.

Why?

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com


From: Pinpress on
Hi Steven,

As you can see, if I need to assign the first element to a variable, I need to have one more assignment statement. That's why I feel not concise enough. Not esthetically appealing to me.

"Steven Lord" <slord(a)mathworks.com> wrote in message <htenrv$qic$1(a)fred.mathworks.com>...
>
> "Pinpress" <nospam__(a)yahoo.com> wrote in message
> news:hte45u$rqt$1(a)fred.mathworks.com...
> > Hi,
> >
> > I need to choose the first element of a vector that's modified by a
> > conditional statement. For example, suppose:
> >
> > I = [1 2 3 4];
> > R = logical( [0 1 0 1] );
> >
> > Then I(R) returns [2 4]. What I need to do is to find "2" and assign it to
> > a new variable, which has to be a one element. I can do the following:
> >
> > t = I(R); t = t(1);
> >
> > But this is not concise enough.
>
> Why?
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
>