Functions and For loops

1 次查看(过去 30 天)
Abdullah Tamimi
Abdullah Tamimi 2011-10-6
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)

采纳的回答

Meh
Meh 2011-10-6
I am not sure if that is what you are looking for. This function will display numbers from 1 to n. copy and paste this on a new M-file and save it with the name 'until'.
function [x]=until(n)
n=input('enter the number:'); x=[1:1:n]
  2 个评论
Walter Roberson
Walter Roberson 2011-10-6
Why are you ignoring the perfectly good value of n that is passed in as an argument?
Jan
Jan 2011-10-6
@Meh: You really want to post an answer. Then I suggest "1:n" instead of "[1:1:n]".

请先登录,再进行评论。

更多回答(2 个)

Andrei Bobrov
Andrei Bobrov 2011-10-6
  7 个评论
Walter Roberson
Walter Roberson 2011-10-6
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
Jan 2011-10-6
@Sean de: I did not understand, that the function must be called "until". I called it "f" and counted the line break.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2011-10-6
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.

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by