More help with probability...

Hey, so I fixed the first part of this code, but now the for loop where I use "strcmpi" is not working properly. It's only accounting for "peach' and my peach_count returns 100000 and the other fruits are 0. It's really weird because my function works fine.
function x = produce
A = 1e5;
B = rand;
C = ceil(A*B);
if (C>=1 && C<= 20000);
C = 'peach';
elseif (C>= 20001 && C<= 55000);
C = 'panana';
elseif (C >= 55001);
C = 'papaya';
end
x = C
clc; clear all;
peach_count = 0; panana_count = 0; papaya_count = 0;
N = 1e6;
for k = 1:N
fruit = produce;
if strcmpi(fruit,'peach')
peach_count = peach_count + 1;
elseif strcmpi(fruit, 'panana')
panana_count = panana_count + 1;
elseif strcmpi(fruit, 'papaya')
papaya_count = papaya_count + 1;
end
end
What am I doing wrong??

6 comentarios

Matt Kindig
Matt Kindig el 24 de Abr. de 2013
Strange...your code works correctly for me. Have you tried just restarting Matlab and re-running it?
Nadia
Nadia el 24 de Abr. de 2013
It runs correctly, however it's not correctly distributing the results in the for loop. I cleared the bottom section and only worked with the function and sure enough, when I type produce to see what it returns, it returns 'peach' every time even though that's not the correct answer.
Matt Kindig
Matt Kindig el 24 de Abr. de 2013
Again, that's strange because I tried your entire code (both the function produce and the for loop), and I got reasonable counts for peach_count, papaya_count, and panana_count. When I called 'produce' several times, I get various values of 'peach', 'papaya', and 'pananan'.
Is it possible that you have multiple definitions of 'produce' somewhere? What does:
which produce
whos produce
give you?
Nadia
Nadia el 24 de Abr. de 2013
This is so strange. Which produce gives me my user name for the computer. Whos produce gave me nothing. I just decided to start over and copy this code back into matlab and now it says the my 'fruit = produce' line is invalid...I don't what I'm doing wrong. I'll try to play with it a bit more.
Matt Kindig
Matt Kindig el 24 de Abr. de 2013
To explain a little bit about why I was having you do those commands:
  • 'which produce' indicates the path to the produce.m file. I was specifically curious whether you had multiple 'produce.m' files on your path, which your results indicate you do not.
  • 'whos produce' indicates information about any variables 'produce' in your workspace. I had thought that perhaps you had a variable called 'produce' as well, which was overriding the function call. However, the fact that the whos command returned nothing indicates this is not the case.
I must admit that I'm stumped. Are you putting the function into a separate file as the script? In other words, you should have two different m-files:
=== produce.m
function C = produce
A = 1e5;
B = rand;
C = ceil(A*B);
if (C>=1 && C<= 20000);
C = 'peach';
elseif (C>= 20001 && C<= 55000);
C = 'panana';
elseif (C >= 55001);
C = 'papaya';
end
=== main.m (or some other name)
clc; clear;
peach_count = 0; panana_count = 0; papaya_count = 0;
N = 1e6;
for k = 1:N
fruit = produce;
if strcmpi(fruit,'peach')
peach_count = peach_count + 1;
elseif strcmpi(fruit, 'panana')
panana_count = panana_count + 1;
elseif strcmpi(fruit, 'papaya')
papaya_count = papaya_count + 1;
end
end
Nadia
Nadia el 25 de Abr. de 2013
@Matt Kindig thanks so much!! I hadn't put them in separate m-files. Such a silly mistake, but thanks again!

Iniciar sesión para comentar.

 Respuesta aceptada

Ahmed A. Selman
Ahmed A. Selman el 24 de Abr. de 2013

0 votos

Just pay attention to how you use function call inside a program. Either you have two files as stated above, or use one main (calling function) which contains another (inner) function as:
% This is the main (calling) function
function output=firstFunction
peach_count = 0; panana_count = 0; papaya_count = 0;
N = 1e6;
for k = 1:N
fruit = produce;
if strcmpi(fruit,'peach')
peach_count = peach_count + 1;
elseif strcmpi(fruit, 'panana')
panana_count = panana_count + 1;
elseif strcmpi(fruit, 'papaya')
papaya_count = papaya_count + 1;
end
end
output=[peach_count;panana_count;papaya_count];
% Here is the inner function
function x = produce
A = 1e5;
B = rand;
C = ceil(A*B);
if (C>=1 && C<= 20000);
C = 'peach';
elseif (C>= 20001 && C<= 55000);
C = 'panana';
elseif (C >= 55001);
C = 'papaya';
end
x = C;
So, when you run:
OutPut=firstFunction
OutPut=
200191
349241
450568
Note: The output varies since it's based on a random creation.
Regards

Más respuestas (0)

Categorías

Más información sobre Desktop en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 24 de Abr. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by