i can not figure out what is wrong with my code,when i call my function it does show eny outpout
1 次查看(过去 30 天)
显示 更早的评论
function [out,temp]=zari(N)
x1=0; %to plithos Z1
x2=0; %to plithos Z2
x3=0; %to plithos Z3
x4=0; %to plithos Z4
sum1=0;
sum2=0;
sum3=0;
sum4=0;
temp= rand(1,N);
for i=1:N
if temp(i)== 10
out(i)=Z1;
x1=x1+1;
sum1=sum1+out(i);
%Z1 edra zariou = 10
else
if temp(i) == 20
out(i)=Z2;
x2=x2+1;
sum2=sum2+out(i);
%Z2 edra zariou = 20
else
if temp(i) == 30
out(i)=Z3;
x3=x3+1;
sum3=sum3+out(i);
%Z3 edra zariou = 30
else
if temp(i) == 40
out(i)=Z4;
x4=x4+1;%Z4 edra zariou = 40
sum4=sum4+out(i);
end
end
end
end
end
end
0 个评论
采纳的回答
David Sanchez
2022-1-14
编辑:David Sanchez
2022-1-14
Hi Tasos, your nested "ff" conditions only contemplate cases for temp(i) equal to 10, 20, 30 or 40. When temp(i) is differente to any of those values, you have not especified the output.
Besides, your input parameter N is used to create a random 1x N array of number. The function rand(1,N) will return N random numbers in the range [0,1], for wich your temp(i) will always be in the case not contemplated by your nested "if" conditions.
To make you see it clearer, here I send you your code with a simple added line that makes your code work ;):
function [out,temp]=zari(N)
x1=0; %to plithos Z1
x2=0; %to plithos Z2
x3=0; %to plithos Z3
x4=0; %to plithos Z4
sum1=0;
sum2=0;
sum3=0;
sum4=0;
temp= rand(1,N);
for i=1:N
if temp(i)== 10
out(i)=Z1;
x1=x1+1;
sum1=sum1+out(i);
%Z1 edra zariou = 10
else
if temp(i) == 20
out(i)=Z2;
x2=x2+1;
sum2=sum2+out(i);
%Z2 edra zariou = 20
else
if temp(i) == 30
out(i)=Z3;
x3=x3+1;
sum3=sum3+out(i);
%Z3 edra zariou = 30
else
if temp(i) == 40
out(i)=Z4;
x4=x4+1;%Z4 edra zariou = 40
sum4=sum4+out(i);
else
out(i) = 666; % ADDED TO MAKE YOU SEE YOUR MISTAKE
end
end
end
end
3 个评论
David Sanchez
2022-1-14
As I told you,
temp= rand(1,N);
will create random numbers whose value is in the range [0 , 1], they will never be greater than 1: you'll never get inside your if conditions.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!