Indexing in "for loops"

13 views (last 30 days)
Charles Ofori
Charles Ofori on 9 Feb 2021
Commented: Charles Ofori on 9 Feb 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

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 9 Feb 2021
Edited: KALYAN ACHARJYA on 9 Feb 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 Comment
Charles Ofori
Charles Ofori on 9 Feb 2021
Hi @KALYAN ACHARJYA, it worked. I had to remove "clear" in "try_2". Thanks for the help

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!