Matlab cannot produce result
이전 댓글 표시
.
function Y = myFunction(d,n,theta)
Y = coinflip(100,100,0.25);
d = 100;
n = 100;
theta = 0.25;
Y = [ ];
for Dloop = 1:d
for Nloop = 1:n
X =randn(1);
Y = [Y;X];
end
end
Y(Y<theta) = 0;
Y(Y>theta) = 1;
histogram(Y)
(I get some errors as below -)
function Y = myFunction(d,n,theta)
↑
Error: Function definition not supported in this context.
Create functions in code file.
(another error is as below)
Error in myFunction (line 2)
Y = coinflip(100,100,0.25);
(other error is as below)
Y = coinflip(100,100,0.25);
Undefined function or variable 'coinflip'.
.
Here please help me to modify the errors and able to produce one correct output graph
.
댓글 수: 15
sloppydisk
2018년 5월 4일
The error messages are very descriptive. You cannot create a function in the command window and coinflip is not defined.
You probably meant to do this:
d= 100;
n = 100;
theta = 0.25;
output = coinflip(d,n,theta);
function Y = coinflip(d,n,theta)
Y = zeros(d*n, 1);
for Nloop = 1:n
for Dloop = 1:d
Y(Nloop*Dloop) = rand;
end
end
Y(Y<theta) = 0;
Y(Y>theta) = 1;
histogram(Y)
end
You should also preallocate Y as I showed. Not sure why you want a nested for loop or what your plans are though.
sloppydisk
2018년 5월 4일
That's because you pasted it into the command window. You can't define functions in there, so instead you should save the code as an m-file and execute it in the editor.
Image Analyst
2018년 5월 4일
Then define it. It's not built into MATLAB, so you need to define what that function does.
sloppydisk
2018년 5월 4일
Much love to you my man, but I hope you will adopt a better attitude, because I don't think you're going to learn much this way.
vokoyo
2018년 5월 4일
Image Analyst
2018년 5월 4일
He already did help you and wrote some code that seems like it does what you want to do. I ran it (the attached code) and it ran fine. What did you do differently????
sloppydisk
2018년 5월 4일
He pasted it into the command window :D
vokoyo
2018년 5월 4일
Image Analyst
2018년 5월 5일
There are no negative numbers. There is a bar centered at 0 with 8000 counts, and one centered at 1 with 2000 counts. If you want the x axis to show only the bar centers, then add this line as the last line of your coinflip() function:
xticks(0:1)
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Modulation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!