how to concisely choose the first element in a vector modified by a conditional statement?

  • Follow


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!
0
Reply Pinpress 5/24/2010 2:58:06 PM

"Pinpress" <nospam__@yahoo.com> wrote in message <hte45u$rqt$1@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
0
Reply us 5/24/2010 3:07:08 PM


Thanks us! Your replies are always good ... it is concise enough for me.

"us " <us@neurol.unizh.ch> wrote in message <hte4ms$42h$1@fred.mathworks.com>...
> "Pinpress" <nospam__@yahoo.com> wrote in message <hte45u$rqt$1@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
0
Reply Pinpress 5/24/2010 3:16:08 PM

Pinpress wrote:

> 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);

t = I(find(R,1))
0
Reply Walter 5/24/2010 3:21:05 PM

"Pinpress" <nospam__@yahoo.com> wrote in message 
news:hte45u$rqt$1@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@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 


0
Reply Steven 5/24/2010 8:34:18 PM

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@mathworks.com> wrote in message <htenrv$qic$1@fred.mathworks.com>...
> 
> "Pinpress" <nospam__@yahoo.com> wrote in message 
> news:hte45u$rqt$1@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@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 
> 
0
Reply Pinpress 5/25/2010 2:12:04 PM

"Pinpress" <nospam__@yahoo.com> wrote in message <hte45u$rqt$1@fred.mathworks.com>...

> 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!
=============


What would be wrong with   t=I(R(1));
0
Reply Matt 5/25/2010 2:37:04 PM

That doesn't work: suppose R = logical([0 0 1 1]).

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message 
> 
> 
> What would be wrong with   t=I(R(1));
0
Reply Pinpress 5/25/2010 2:50:20 PM

"Pinpress" <nospam__@yahoo.com> wrote in message <htgo3c$d6v$1@fred.mathworks.com>...
> That doesn't work: suppose R = logical([0 0 1 1]).
> 
> "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message 
> > 
> > 
> > What would be wrong with   t=I(R(1));
==============

Sorry, you're right. Well, although it's off-label use, you could use my IndexableFunction class

http://www.mathworks.com/matlabcentral/fileexchange/26570-direct-indexing-of-function-calls-oop-exercise

It would work as follows:

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

>> t=IndexableFunction(I);

>> t{R}(1) %almost the syntax that you want


ans =

     2
0
Reply Matt 5/25/2010 3:30:27 PM

8 Replies
204 Views

(page loaded in 0.416 seconds)

Similiar Articles:




7/11/2012 2:36:18 PM


Reply: