Matlab cannot produce result
1 次查看(过去 30 天)
显示 更早的评论
vokoyo
2018-5-4
.
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
I understand but I need to finish my task urgently
Other people they talk so many but mostly useless
Please help as best as you can
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????
vokoyo
2018-5-4
编辑:vokoyo
2018-5-4
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)
Now I can run but the output graph why show some negative numbers?
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)
vokoyo
2018-5-5
编辑:vokoyo
2018-5-5
Matlab Programming - I am not sure how to modify it to get the two graphs in Question 2
(1)
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 =rand(1);
Y = [Y;X];
end
end
Y(Y<theta) = 0;
Y(Y>theta) = 1;
histogram(Y)
xticks(0:1)
function Y = myFunction(d,n,theta)
Y = coinflip(100,100,0.5);
d = 100;
n = 100;
theta = 0.5;
Y = [ ];
for Dloop = 1:d
for Nloop = 1:n
X =rand(1);
Y = [Y;X];
end
end
Y(Y<theta) = 0;
Y(Y>theta) = 1;
histogram(Y)
xticks(0:1)
(2)
function Y = myFunction(d,n,theta)
Y = coinflip(10,1000,0.25);
d = 10;
n = 1000;
theta = 0.25;
Y = [ ];
for Dloop = 1:d
for Nloop = 1:n
X =rand(1);
Y = [Y;X];
end
end
Y(Y<theta) = 0;
Y(Y>theta) = 1;
histogram(Y)
xticks(0:1)
function Y = myFunction(d,n,theta)
Y = coinflip(10,1000,0.5);
d = 10;
n = 1000;
theta = 0.5;
Y = [ ];
for Dloop = 1:d
for Nloop = 1:n
X =rand(1);
Y = [Y;X];
end
end
Y(Y<theta) = 0;
Y(Y>theta) = 1;
histogram(Y)
xticks(0:1)
(3)
function Y = myFunction(d,n,theta)
Y = coinflip(100,1000,0.25);
d = 100;
n = 1000;
theta = 0.25;
Y = [ ];
for Dloop = 1:d
for Nloop = 1:n
X =rand(1);
Y = [Y;X];
end
end
Y(Y<theta) = 0;
Y(Y>theta) = 1;
histogram(Y)
xticks(0:1)
function Y = myFunction(d,n,theta)
Y = coinflip(100,1000,0.5);
d = 100;
n = 1000;
theta = 0.5;
Y = [ ];
for Dloop = 1:d
for Nloop = 1:n
X =rand(1);
Y = [Y;X];
end
end
Y(Y<theta) = 0;
Y(Y>theta) = 1;
histogram(Y)
xticks(0:1)
回答(0 个)
另请参阅
标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)