# for loop/periodic implementation for number or arrays?

13 views (last 30 days)
Hatim Altarteer on 31 Aug 2021
Commented: Hatim Altarteer on 31 Aug 2021
i have 8 arrays of 100x3 configuration. i want to make a for loop (or another method is also appreciated) to accont for 2 constraints.
if the value of a number in the array is less than 1, replace it with one. also, if a number in the array is greater than nx + 1, put it back to 1. (nx is the upper limit, say nx = 100)
for instance,
array1 = [x0,y0,z0];
array2 = [x0,y0,z1];
array3 = [x0,y1,z0];
array4 = [x0,y1,z1];
array5 = [x1,y0,z0];
array6 = [x1,y0,z1];
array7 = [x1,y1,z0];
array8 = [x1,y1,z1];
where xyz0 and xyz1 are coordinates of a particel in the xyz plane. the attached file is an example of x0 where it includes the location of the "x" coordinates of 100 particels. the same applies for the rest of yz0 and xyz1.

Fabio Freschi on 31 Aug 2021
If you want to force the max and min values of your vectors, you can preprocess them
x0(x0 < 1 & x0 > nx+1) = 1;
If the boundary value is different youy can split the instruction in two
x0(x0 < 1) = 1;
x0(x0 > nx+1) = nx+1; % for example
repeat for y0 z0 x1 y1 z1 and then create your 8 arrays
Hatim Altarteer on 31 Aug 2021
Thanks Fabio!

DGM on 31 Aug 2021
Let's say you have an array A:
A = randi([-5 110],5,3) % random test array
A = 5×3
77 87 95 110 34 87 18 27 89 7 -3 8 67 2 37
allowedrange = [1 101]; % min and max value allowed
A = min(max(A,allowedrange(1)),allowedrange(2))
A = 5×3
77 87 95 101 34 87 18 27 89 7 1 8 67 2 37
That will clamp the values to keep them within a specified range.
Hatim Altarteer on 31 Aug 2021
thanks for the explanation! really appreciated

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!