From: Ralph Mettier on 2 Aug 2010 04:30 I recently ran into a question from a new Matlab user, that totally stumped me. Not only couldn't I fin the answer in the help files, I frankly can't even think of a way to formulate the question for a search. So I turn to you guys: We all use functions that return 1D arrays (row or column vector, doesn't matter), for instance commas=findstr('one, two, three',','); Now in this example, if I want the first occurance, I'll usually add another line like: firstcomma=commas(1); and this works fine. But is it possible to do that in one? Is there something like firstelement(vector) which would return the first element of whatever the inner function returns? I realize that something like this would be exceedingly simple to write, but 1) why re-invent the wheel, especially when the original wheelmaker make much better wheel than I do, an 2) 'write it yourself' isn't a particularly helpful answer for a new user. So? Any ideas?
From: Joshua Arnott on 2 Aug 2010 05:20 "Ralph Mettier" <itsmyspam(a)gmail.com> wrote in message <i35vmt$c8r$1(a)fred.mathworks.com>... > I recently ran into a question from a new Matlab user, that totally stumped me. Not only couldn't I fin the answer in the help files, I frankly can't even think of a way to formulate the question for a search. So I turn to you guys: > > We all use functions that return 1D arrays (row or column vector, doesn't matter), for instance > > commas=findstr('one, two, three',','); > > Now in this example, if I want the first occurance, I'll usually add another line like: > > firstcomma=commas(1); > > and this works fine. But is it possible to do that in one? Is there something like > > firstelement(vector) which would return the first element of whatever the inner function returns? > I realize that something like this would be exceedingly simple to write, but 1) why re-invent the wheel, especially when the original wheelmaker make much better wheel than I do, an 2) 'write it yourself' isn't a particularly helpful answer for a new user. > > So? Any ideas? What you're describing is called cascading indexing. It's supported in some languages (e.g., Perl) but not in Matlab. Have a search for previous threads to get the full argument, but basically it's leads to less-legible code, and common syntax implementations clash with existing Matlab syntax. Josh.
From: Walter Roberson on 2 Aug 2010 11:47 Ralph Mettier wrote: > We all use functions that return 1D arrays (row or column vector, > doesn't matter), for instance > commas=findstr('one, two, three',','); > > Now in this example, if I want the first occurance, I'll usually add > another line like: > > firstcomma=commas(1); > > and this works fine. But is it possible to do that in one? Is there > something like > > firstelement(vector) which would return the first element of whatever > the inner function returns? > I realize that something like this would be exceedingly simple to write, > but 1) why re-invent the wheel, especially when the original wheelmaker > make much better wheel than I do, an 2) 'write it yourself' isn't a > particularly helpful answer for a new user. The built-in function for this is subsref() subsref(findstr('one, two, three',','), struct('type','[]', 'subs', {{1}})) This is a case where it is much easier to write your own function: firstelement = @(v) v(1); firstelement( findstr('one, two, three',',') )
|
Pages: 1 Prev: Does simhydraulic work with steam? Next: to test a regression model |