Performing conditionals on values in a matrix>

1 visualizzazione (ultimi 30 giorni)
eel
eel il 8 Feb 2018
Risposto: Walter Roberson il 8 Feb 2018
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?

Risposte (1)

Walter Roberson
Walter Roberson il 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;

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by