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
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
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!