Create an .m file

조회 수: 36 (최근 30일)
Benjamin Trivers
Benjamin Trivers 2020년 1월 17일
편집: Adam Danz 2020년 1월 19일
Create an .m file that is a function that creates an array of N random integers in the range from -9999 to 9999. It should be in the form of x=randint(N).
NOTE: This time, upload as an m file, not as a published Word document. You'll be using these functions in a later quiz. But you should test that it works on your own because I will be verifying it in MATLAB when I grade.
Create an .m file that is a function that finds the maximum value in an array of numbers. It should be in the form of max=maxval(x). Do not use built in function max.
NOTE: This time, upload as an m file, not as a published Word document. You'll be using these functions in a later quiz. But you should test that it works on your own because I will be verifying it in MATLAB when I grade.
I have watched the video for this class multiple times and have zero ideas of how to tackle this. These are the first couple of question for the section and I think that if i got helpe on these ones, I could do the rest.
  댓글 수: 3
Benjamin Trivers
Benjamin Trivers 2020년 1월 18일
function x = randint(N)
% you fill in code here that creates x from the input variable N
x=[];
for i=0:N
x=[x,randi(0,9999,N)-9999];
end
end
this is what I have
Adam Danz
Adam Danz 2020년 1월 19일
편집: Adam Danz 2020년 1월 19일
Close!
You're correct to use randi() but your syntax is off. Check out the syntax options.
Yours should look like randi([min,max],[1,N]).
And you don't need the loop since the line above will produces all N values at once.

댓글을 달려면 로그인하십시오.

답변 (1개)

James Tursa
James Tursa 2020년 1월 17일
편집: James Tursa 2020년 1월 17일
To create an .m file for a function named randint, you can do this at the command line as long as the default directory is your working directory:
edit randint.m
That starts the editor. Now you can enter the lines of code for the function:
function x = randint(N)
% you fill in code here that creates x from the input variable N
end
When you are done be sure to save it.
To test the function, just call it from the command line. E.g.,
>> x = randint(10)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by