how access structure of array element using loop

조회 수: 1 (최근 30일)
Sanjoy Dey
Sanjoy Dey 2019년 3월 24일
편집: Stephan 2019년 3월 24일
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