how access structure of array element using loop

1 vue (au cours des 30 derniers jours)
Sanjoy Dey
Sanjoy Dey le 24 Mar 2019
Modifié(e) : Stephan le 24 Mar 2019
Firstly i'm beginner in matlab,i don't have enough knowledge to write code in matlab.
Can i access Structure of array element using for loop?when i run this code,going to infinite loop i can't understand what's the problem.Please correction my code.
% Structure of Array of user set
user(1).will = 0.5;
user(1).rep = 1;
user(1).bid = 5;
user(1).inter_time=4;
user(2).will = 0.9;
user(2).rep = 0.6;
user(2).bid = 6;
user(2).inter_time=7;
% Structure of Arrayof task set
task(1).bud = 8;
task(1).qua = 1;
task(2).bud = 9;
task(2).qua = 2;
task(3).bud = 7;
task(3).qua = 1;
for i=1:2
for j=1:3
while(user(i).bid<=task(j).bud)
com_data_quality = (user(i).will*user(i).rep/user(i).bid*user(i).inter_time);
fprintf('%f value is\n', com_data_quality);
end
end
end
  1 commentaire
Walter Roberson
Walter Roberson le 24 Mar 2019
You have a while loop, but inside the while loop you do not change any of the variables involved in the while test. You do not change i, you do not change j, you do not change user(i).bid, you do not change task(j).bud . There while loop as no reason to terminate.

Connectez-vous pour commenter.

Réponse acceptée

Stephan
Stephan le 24 Mar 2019
Modifié(e) : Stephan le 24 Mar 2019
Hi,
your loop start with i=1 & j=1. Compare the conditions of your while loop with this initial case. you dont change bid or bud insinde the while loop. So this is why it runs forever.
Note that i&j are not recommended as loop counters, since they are used for complex numbers in Matlab.
You could try to write this code without for loop, but for starting maybe it is better to debug what you have written so far and get it to run. Vectorizing code could be an improvement you try later.
Best regards
Stephan

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by