Performing conditionals on values in a matrix>

2 views (last 30 days)
I have a function that is supposed to take an input of a matrix, A, which is in the form: [x y z; x y z; ...]. I extract each x, y, and z values using:
x = A(1:end,1);
y = A(1:end,2);
z = A(1:end,3);
This works fine when I perform the calculations finding the distance from the origin to this inputted point:
x_square = x.^2;
y_square = y.^2;
z_square = z.^2;
total = x_square + y_square + z_square;
d = total.^(1/2);
However, I need to check and see if the input values fall within these ranges: 0 <= x <= 375, -50 <= y <= 50, 0 <= z <= 100
How can I perform conditionals on the columns of the matrix, and so that if it fails to follow these conditionals, output that d = 0?

Answers (1)

Walter Roberson
Walter Roberson on 8 Feb 2018
d = sqrt(sum(A.^2, 2));
mask = 0 < A(:,1) & A(:,1) <= 375 & -50 < A(:,2) & A(:,2) <= 50 & 0 <= A(:,3) & A(:,3) <= 100;
d(mask) = 0;

Categories

Find more on Creating and Concatenating Matrices 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!