Borrar filtros
Borrar filtros

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

1 visualización (últimos 30 días)
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

Respuesta aceptada

David Sanchez
David Sanchez el 14 de En. de 2022
Editada: David Sanchez el 14 de En. de 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
out(i) = 666; % ADDED TO MAKE YOU SEE YOUR MISTAKE
end
end
end
end
  3 comentarios
Tasos Apostolopoulos
Tasos Apostolopoulos el 14 de En. de 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
David Sanchez el 14 de En. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by