4 views (last 30 days)

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??

Ahmed A. Selman
on 24 Apr 2013

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

Opportunities for recent engineering grads.

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

Start Hunting!
## 6 Comments

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/73424-more-help-with-probability#comment_145314

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/73424-more-help-with-probability#comment_145314

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/73424-more-help-with-probability#comment_145324

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/73424-more-help-with-probability#comment_145324

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/73424-more-help-with-probability#comment_145325

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/73424-more-help-with-probability#comment_145325

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/73424-more-help-with-probability#comment_145327

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/73424-more-help-with-probability#comment_145327

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/73424-more-help-with-probability#comment_145329

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/73424-more-help-with-probability#comment_145329

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/73424-more-help-with-probability#comment_145584

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/73424-more-help-with-probability#comment_145584

Sign in to comment.