how access structure of array element using loop

1 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2019-3-24
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.

请先登录,再进行评论。

采纳的回答

Stephan
Stephan 2019-3-24
编辑:Stephan 2019-3-24
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by