Indexing in "for loops"

4 Ansichten (letzte 30 Tage)
Charles Ofori
Charles Ofori am 9 Feb. 2021
Kommentiert: Charles Ofori am 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

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by