필터 지우기
필터 지우기

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

채택된 답변

David Sanchez
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
Tasos Apostolopoulos
Tasos Apostolopoulos 2022년 1월 14일
never mind i get what you are saying but still when i add out(i)=0; now the outpout shows only 0 0( zeros) is something wrong with my if coditions?
David Sanchez
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개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by