COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

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

• Email
• 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

See related articles to this posting

```"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

```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

```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

```"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
http://www.mathworks.com

```
 0

```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
> http://www.mathworks.com
>
```
 0

```"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

```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

```"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

8 Replies
274 Views

Similar Articles

12/11/2013 12:45:12 AM
page loaded in 2406 ms. (0)

Similar Artilces:

How do I do a conditional statement in a constant statement?
I'd like to do the equivalent of constant X : integer = (A > B) ? A : B; in VHDL, where A and B are either constants of generics Obviously VHLD does not use the "?" operator, but I tried things such as constant X : integer = A when (A>B) else B; and that doesn't work. This is quite straightforward with Verilog Parameters so it is not unreasonable to do this. Is it possible in VHDL ? tkvhdl@gmail.com wrote: > I'd like to do the equivalent of > constant X : integer = (A > B) ? A : B; > Is it possible in VHDL ? Yes, but I have to write the function mys...

Modifying Element In For List
My question concerns elementary list and pass by reference: I've written a function which is passed a list that contains rows read from a csv file. The function traverses csv_rows, row by row, and inspects the first element in each row. The function tests for '', and if true, replaces that with a 0. I've used the standard Python for syntax for this. def cleanMeterID(csv_rows, bad_meter_id_count): d = drIdx() row_number = 0 for row in csv_rows: if False == is_number(row[d.MeterID]): csv_rows[row_number][d.MeterID] = 0 ...

why not multiple statement modifiers?
Just curious, would it be possible for a future version of Perl to support this sort of statement? print for @a if \$a; or print for @\$_ if /^a/ while(<A>); I imagine that a module could be written to do it: multiples 'print for @a if \$a' where the multiples function would construct a function to execute the statement properly. The problem is that it already looks ugly and the implementation would probably be very difficult. Is there a logic problem with allowing more than one statement modifier? Thanks! wana ioneabu@yahoo.com wrote: > Just curious, would it be po...

date modified if statement
I'm rather new to Perl and its syntax, and I did try multiple searches before posting this, but I am using Perl to create a Macro that will automatically send an e-mail and a specific attachment every time the attachment is updated. What would the syntax be for the if statement for code that would send the attachment if it has been modified in the past 12 hours? Any help would be appreciated. Thanks! Naji wrote: > I'm rather new to Perl and its syntax, and I did try multiple searches > before posting this, but I am using Perl to create a Macro that will > automatically send ...

Simplifying a basic if statement conditional
Is there a more efficient way to write this: if(\$var=3D1 || \$var=3D4 || \$var=3D27 || \$var=3D28 || \$var=3D30 || \$var=3D37= || \$var=3D38){echo "true";} Cheers, Ciar=E1n Ciaran wrote: > Is there a more efficient way to write this: > if(\$var=1 || \$var=4 || \$var=27 || \$var=28 || \$var=30 || \$var=37 || > \$var=38){echo "true";} > > Cheers, > Ciar�n > ='s should be == I guess if(in_array(\$var, array(1, 4 etc))) is perhaps shorter, but not "more efficient", so the answer is "no". -- gosha bine extended php parser ~ http://cod...

How to access elements of a range in a vector??
Hi, I want to do a task of modifying the elements in a range, say 5th element to 10th element.. I also know that there is a function TRANSFORM which is used to transform the elements of any container, say a vector, but in that function, the arguments are iterators, and what I have is a range and not iterators to these elements. Eg vector<int> v; v.transform(v.begin(),v.end(),v.begin(),modifying_func); //this, I want to apply not from begin to end, but from say, element at index i to index j; //similarly, if I want to know the values in a range of vector or list, not from be...

Modifying Array inside While statement
I have a perl script that takes an array and processes the arguments inside it. while (<@my_array>) { print "\$_"; if (/new/i) { @new_array = shift @my_array; } } Now I expect as while loop progresses, the array @my_array shrinks but the while loop get executed for all the indices in @my_array. Say if I have 10 elements in my_array, then the while loop is executed 10 times. There are about six times the 'shift' gets executed but it does not affect the number of iterations of the while loop. What can be the workaround for this? thanks, Anand. Andy...

Avoiding nested IF conditionals,and STOP statement
Hi all, I'm writing a program in Fortran 95 and reading S. Chapman's "Fortran 90/95 for Scientists and Engineers" and I have a problem concerning conditional termination of my program. There are a few points in my program where if the input is not suitable, then I want to terminate the program. For example, if I am opening a number of files, at each one I want to exit the program if the file hasn't opened correctly. Chapman's style is to put IF conditionals one inside the other, avoiding the STOP statement. I find these nested IF's a little bit too much if there...

why std::vector elements are at invalid addresses
hi, i got a program that is suppose to run 24x7x365. However after functioning for around 15 minutes, it crashes due to a segmentation fault. program is written in c++ and runs in RH Linux 4. When inspected one of the parameters of the crashing frame, i get the following output. NOTE : c1 is a std::vector (gdb) pvector c1 elem[0]: \$1 = { static npos = 4294967295, _M_dataplus = { <std::allocator<char>> = { <__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, members of std::basic_string<char,std::char_traits<char...

using next or last element in vector
Hi, Basically I have a vetor "a2" which sometimes has zero elements. Whenever a zero element occurs, the best thing to do is give it the value of the nearest non-zero element. This is pretty much always the inboard element, i.e. the next element towards the middle of the vector, however if there is a faster method that doesn't search inwards then I am happy to use it. At the moment I have: while (sum(a2)==0)>=1 for idx=1:length(a2) if a2(idx)==0; if idx/length(a2)<=0.5 a2(idx)=a2(idx+1); ...

Case Conditional in SQL Statement
Hi, I'm trying to do calculations in a SQL statement, but depending on one variable (a.type in example) I'll need to pull another variable from seperate tables. Here is my code thus far: select a.DeptCode DeptCode, a.Type Type, (a.ExpenseUnit / (select volume from TargetData b where b.type = a.type) ) Expense From calc1 a The problem... a.Type can be FYTD, Budget, or Target... and depending on which one it is, I need to make b either FYTDData, TargetData, or BudgetData. I'm thinking a case statement might do the trick, but I can't find any syntax on how to use C...

Re: Number of Observations Read By MODIFY Statement #3
It gets more interesting. Not only are the output statistics misleading, but the processing times aren't consistent. I ran the same type of test, but varied the size of bigger from 1E7 to 7E7, and ran 3 trials for each. data bigger; do key = 1 to 1E7; /* then 2E7, 3E7, 4E7, 5E7, 6E7 and 7E7 */ output; end; run; data smaller; do key = 1,5000,999999; output; end; run; data bigger; modify bigger smaller; by key; remove; run; File Time Size Type Trial 1 Trial 2 Trial 3 ---- --------- ------------ ------------ ------------ 1E7 ...

In Search of Elegant Code
Suppose I have an array like this: my @stuff = ("Just", "Some", "", "", "Text"); I want to replace the first (and ONLY the first) null element with some string. If the array has no null values, don't do anything to it (unless the array is empty, in which case add the string as an element). This almost works: do {\$stuff[\$_] =~ s/^\$/Null/ && last} for ( 0 .. @stuff-1 ) ; but it won't affect an empty array as desired (so I need an extra line to test and push). I can do it REALLY ugly like this: my \$index = 0; un...

Re: PROC REPORT
Jim, Interesting idea. However, my interpretation of the problem is that the LINE statement output is desired for some groups but not others within a single run. I can see how you could generate the LINE statement or not with macro for a PROC REPORT run, based on previous DATA step analysis as you suggested, but I think what's needed is a kludge of some kind so the LINE statement "executes" for some groups but not others within the same PROC REPORT run. Mike Rhoads Westat RhoadsM1@Westat.com -----Original Message----- From: Groeneveld, Jim [mailto:jim.groeneveld@vitatron.com...

Design first or Implementation first???
Hi all, I've always been wondering about how intellectuals design new things. For instance if i were to design a kernel.. The first thing i wud do is. look for a bootloader and its available... so i wud rather go & start implementing the kernel.. Here is my dilemma... chicken & egg question??? Should i be designing the kernel completely and then start implementing (coding) or should i start coding as & when ideas come to me .. Why this question crops up?? when i start designing not many ideas come to my mind.. but when i start implementing... the ideas come as & when the...

Dynamic WHERE IN statement in Cursor SQL statement
I'm trying to dynamically build the values of a WHERE IN clause but I can't seem to get it to work. I'm new to writing DB2 stored procedures and haven't found any examples that show what i'm trying to do. Any help would be appreciated. Below is a sample procedure that captures what I'm trying to do. Thanks. ========= sample procedure ============== CREATE PROCEDURE SP_PROC1( IN VAR-A CHARACTER(3), IN VAR-B CHARACTER(10)) -------------------------------------------------------------------------- -- DB2 SQL procedure ------------------------------...