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일

0 개 추천

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일

1 개 추천

댓글 수: 7

Abdullah Tamimi
Abdullah Tamimi 2011년 10월 6일
that doesn't really help at all...
Jan
Jan 2011년 10월 6일
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
Daniel Shub 2011년 10월 6일
I think an anonymous function could be used allowing for 1 line and 15 characters.
Jan
Jan 2011년 10월 6일
@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
Sean de Wolski 2011년 10월 6일
As a standalone function I haven't been able to get it below 25 :(.
As an anonymous I have 13.
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일

0 개 추천

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.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2011년 10월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by