I've been trying to figure out how to convert this for too long but without success - I'm hoping this is more obvious to a fresh set of eyes. I have a function defined in mathematica:

f=If[-y <= x <= y || y <= -x <= -y, Sign[y], 0]

Which is then applied as:

k_x = A * f;

k_y = A * -f

k_z = 0;

Plotted as a quiver this results in the following on my set of evaluation points (where A is some scalar, =1 for simplicity here):

I know the conditionals need to be seperated out to apply this in MATLAB. My current best attempt is:

N = 3;

nr = length(location.x);

k = zeros(N,nr);

k_x = zeros(1,nr); k_y = zeros(1,nr);

for ind = 1:length(location.x)

if (-location.y(ind) <= location.x(ind) && location.x(ind) <= location.y(ind)) || ...

(location.y(ind) <= -location.x(ind) && -location.x(ind) <= -location.y(ind))

k_x(ind) = sign(location.y(ind));

k_y(ind) = 0;

elseif (location.x(ind) <= -location.y(ind) && -location.y(ind) <= -location.x(ind)) || ...

(-location.x(ind) <= location.y(ind) && location.y(ind) <= location.x(ind))

k_y(ind) = sign(location.x(ind));

k_x(ind) = 0;

else

k_x(ind) = 0;

k_y(ind) = 0;

end

end

k(1,:) = k_x;

k(2,:) = -k_y;

k(3,:) = 0;

figure; quiver3(location.x,location.y,location.z,k(1,:),k(2,:),k(3,:)); hold on;

scatter3(location.x,location.y,location.z,'kx');

This almost works, but the corners don't have the same diagonal as the result I'm expecting from the function (as seen below). The 'diagonal' vectors which I don't achieve in my own function are really important. I also feel like the if loop is probably not the best way to evaluate this function.

An example location structure array is also attached here if testing/applying the code is useful.

Thanks in advance.

## 3 Comments

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/516713-point-categorisation-via-a-suggested-mathematica-if-statement#comment_824689

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/516713-point-categorisation-via-a-suggested-mathematica-if-statement#comment_824689

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/516713-point-categorisation-via-a-suggested-mathematica-if-statement#comment_824766

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/516713-point-categorisation-via-a-suggested-mathematica-if-statement#comment_824766

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/516713-point-categorisation-via-a-suggested-mathematica-if-statement#comment_824780

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/516713-point-categorisation-via-a-suggested-mathematica-if-statement#comment_824780

Sign in to comment.