Indexing in "for loops"

3 vues (au cours des 30 derniers jours)
Charles Ofori
Charles Ofori le 9 Fév 2021
Commenté : Charles Ofori le 9 Fév 2021
I already have a code i want to run multiple times because it has some form of randomisation where every run produces a different value. So basically, i want to generate a list of values. I used the for loop in runnning the script. First i declared an empty array then used a loop to run the random number generator about 100 times. Something like this:
group=ones(100,1);
for i=1:100
run('try_2');
group(i,:)=value;
end
It worked at first but upon subsequent trials, it is no longer working. I get an error message " subscript indices must either be real positive integers or logicals loops "
Is there any way i can get by this. I just want to be able to run my script multiple times and generate a vector or matrix that collects the answers at every run. Thanks

Réponse acceptée

KALYAN ACHARJYA
KALYAN ACHARJYA le 9 Fév 2021
Modifié(e) : KALYAN ACHARJYA le 9 Fév 2021
Here I have considering three assumptions and tried to reproduce the same, there is no error
  1. Suppose "value" is the scalar result
group=zeros(1,100);
for i=1:100
%run('try_2'); % this command return the value, right?
group(i)=value;
end
2. Value is a vector, let say "value" is 1D array with fixed length n
group=zeros(100,n);
for i=1:100
%run('try_2');
group(i,:)=value;
end
3. Value is varrying result as interation progress, in such case use cell array to store the result
group=cell(1,100);
for i=1:100
%run('try_2');
group{i}=value;
end
Rest is OK, just ensure that respective "run" commnad generete the value variable. Still unsolved please provide the detail of try_2 ??
Hope it Helps!
  1 commentaire
Charles Ofori
Charles Ofori le 9 Fév 2021
Hi @KALYAN ACHARJYA, it worked. I had to remove "clear" in "try_2". Thanks for the help

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements 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