for loop/periodic implementation for number or arrays?
13 views (last 30 days)
Show older comments
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)
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.
thanks in advance!
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
More Answers (1)
DGM on 31 Aug 2021
If I'm reading this right...
Let's say you have an array A:
A = randi([-5 110],5,3) % random test array
allowedrange = [1 101]; % min and max value allowed
A = min(max(A,allowedrange(1)),allowedrange(2))
That will clamp the values to keep them within a specified range.
Find more on Matrix Indexing 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!