# i can not figure out what is wrong with my code,when i call my function it does show eny outpout

1 visualizzazione (ultimi 30 giorni)
Tasos Apostolopoulos il 14 Gen 2022
Commentato: David Sanchez il 14 Gen 2022
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 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

David Sanchez il 14 Gen 2022
Modificato: David Sanchez il 14 Gen 2022
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
end
end
end
end
##### 3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Tasos Apostolopoulos il 14 Gen 2022
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 il 14 Gen 2022
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.

Accedi per commentare.

### Categorie

Scopri di più su Programming in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by