Functions and For loops
Show older comments
I need help with my homework, I am stumped on this question.
create a new function (Call it until) that takes as an argument an intefer n and spits out the vector x=(1,2,3,...,n)
Accepted Answer
More Answers (2)
Andrei Bobrov
on 6 Oct 2011
1 vote
7 Comments
Abdullah Tamimi
on 6 Oct 2011
Jan
on 6 Oct 2011
What would "really" help? The question is very basic and the solution has 2 lines only, a total of 22 characters! If we post the solution, you cannot claim to have solved the question by your own. Of course your teacher knows this forum also and (s)he will not like cheating.
I suggest to read the Getting Started chapters in the documentation. You find all needed information there: 1. how to create a function, 2. how to create the vector [1,2,3, ..., n].
Daniel Shub
on 6 Oct 2011
I think an anonymous function could be used allowing for 1 line and 15 characters.
Jan
on 6 Oct 2011
@Daniel: I think "create a new function" means, that the teacher wants to see the term "function" appearing in the solution. Actually the colon-operator solves the problem already, but it is not "new".
Sean de Wolski
on 6 Oct 2011
As a standalone function I haven't been able to get it below 25 :(.
As an anonymous I have 13.
Walter Roberson
on 6 Oct 2011
Sean, are you including a trailing semi-colon for the anonymous function definition? And are you keeping to the requirement that the created function be named "until" and that it must take an argument and that argument must be named "n" ?
I get 13 only if I exclude the semi-colon.
Jan
on 6 Oct 2011
@Sean de: I did not understand, that the function must be called "until". I called it "f" and counted the line break.
Walter Roberson
on 6 Oct 2011
0 votes
It's been a long time since I encountered a computer language with a "spit" operation, and I never did get to use it myself. I believe that in the early 1970's, one of the FORTRAN variant compilers produced at University of Waterloo had a SPIT command along with PRINT and PUNCH, intended for use with punching paper tape. There is, however, countervailing historical claim that the IBM computers that those versions of FORTRAN ran on, never had an accessible paper tape punch.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!