for loop/periodic implementation for number or arrays?

2 次查看(过去 30 天)
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.
thanks in advance!

采纳的回答

Fabio Freschi
Fabio Freschi 2021-8-31
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

更多回答(1 个)

DGM
DGM 2021-8-31
If I'm reading this right...
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.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by