Vector from loop - stop after x numbers of iterations

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

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.
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 ?
Hi Walter, I'm afraid I didn't explain myself clearly. Yes, I'd like to create a new vector that stops at the end of trial 6, as you say. Thanks
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?
Sorry, that would be 7*7, yes.
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.

Sign in to comment.

Answers (0)

Categories

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

Products

Release

R2021b

Asked:

on 11 Feb 2023

Commented:

on 13 Feb 2023

Community Treasure Hunt

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

Start Hunting!