Vector from loop - stop after x numbers of iterations
Show older comments
Hi,
I have this loop here:
n_trial = 12;
n_probe =7;
longer_array = [1:96];
updated_probeNum_withinBlock=0;
for trial = 1:n_trial
for probe = 1:n_probe
probeNum_withinBlock=longer_array((trial-1)*n_probe+probe+updated_probeNum_withinBlock); %index into your longer_array
end
probeNum_withinBlock= (probeNum_withinBlock+1);
updated_probeNum_withinBlock= (1+updated_probeNum_withinBlock);
end
I need a similar vector to "probeNum_withinBlock" but that stops after the 48th iteration (e.g., I need this vector to be a 1 x 48 array, from 1 to 48). It seems that I cannot find the correct way to do this.
Any help would be very much appreciated.
7 Comments
Walter Roberson
on 11 Feb 2023
for probe = 1:n_probe
probeNum_withinBlock=longer_array((trial-1)*n_probe+probe+updated_probeNum_withinBlock); %index into your longer_array
end
That appears to be overwriting all of probeNum_withinBlock every iteration, so the final result would be the same as if you had only done for probe = n_probe . Perhaps the desired output variable should be updated_probeNum_withinBlock ? Something looks wrong with the logic.
Walter Roberson
on 11 Feb 2023
probeNum_WithinBlock is a scalar in the above code, so I am not clear as to what you are asking for?
Are you saying that you want to store the results of probeNum_WithinBlock, except that you want to stop recording after probe #7 of trial #6 ? since 6*nprobe+7 == 48 ?
Mikel Jimenez
on 11 Feb 2023
Walter Roberson
on 11 Feb 2023
Each trial does nprobe=7 iterations. 6 trials would be 6*7=42 iterations, 7 trials would be 7*7 = 49 iterations. Do you need to stop recording after probe 6 of trial 7?
Mikel Jimenez
on 12 Feb 2023
Walter Roberson
on 12 Feb 2023
n_trial = 12;
n_probe =7;
longer_array = [1:96];
updated_probeNum_withinBlock=0;
for trial = 1:n_trial
for probe = 1:n_probe
probeNum_withinBlock=longer_array((trial-1)*n_probe+probe+updated_probeNum_withinBlock); %index into your longer_array
if trial <= 7
probe_record(probe,trial) = probeNum_withinBlock;
end
end
probeNum_withinBlock= (probeNum_withinBlock+1);
updated_probeNum_withinBlock= (1+updated_probeNum_withinBlock);
end
probe_record = probe_record(:); %make it a vector
But unless n_trial is a lot larger than the cutoff point (7), most people would simply record all of the outputs and then later throw away the ones they do not need.
Mikel Jimenez
on 13 Feb 2023
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!