MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Lucas
on 19 Jan 2013

Hey! I would like to create a kind of switch that allows the exsistence of a signal under specific circumstances in MatLab. There are about 91.000 checkpoints in which the switch has to operate to give an answer to the question: does contact exsist? It should say either "yes" (1) or "no" (0) for each of these 91.000 checkpoints.

There is the position of a tool which is declared to be Xsollf(i),Ysollf(i),Zsollf(i) which is variable in time and the position data of a work piece which reaches from X1 to X2, Y1 to Y2 and Z1 to Z2 which is fixed.

So now i tried something like this:

cuttingforce(i) = zeros(1,length(Tsoll)); % Tsoll is the vector of time

for i = 2:length(Tsoll)

if Z1<Zsollf(i) && Zsollf(i) <Z2 && Y1<Ysollf(i) && Ysollf(i) <Y2 && X1<Xsollf(i) && Xsollf(i)<X2 cuttingforce(i)=1; end % of if end %of for

I wanted to use &&-operator that saves a little bit of time by stopping the examination whenever one of these if conditions is flase. Each time the if conditions are true which means the tool is on the surface of the work piece or within its body the element of the cutting force vector should be changed from 0 to 1 so that there can exsist cutting force.

I am just an amateur and by now I just can not find a solution. I would be grateful to read your ideas.

Answer by Image Analyst
on 19 Jan 2013

Try this vectorized way:

cuttingforce = zeros(1, length(Tsoll)); Fzerspan = zeros(1, length(Tsoll)) inRange = Z1 < Zsollf(i) && Zsollf(i) < Z2 &&... Y1 < Ysollf(i) && Ysollf(i) < Y2 &&... X1 < Xsollf(i) && Xsollf(i) < X2) % If in range, set Fzerspan and cuttingforce. Fzerspan(inRange) = 1; cuttingforce(inRange) = 1;

## 3 Comments

## Lucas (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/59281#comment_123623

Heureka! Found the answer myself!

## Jan Simon (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/59281#comment_123672

The pre-allocation is important for efficient code:

Not

cuttingforce(i).## Image Analyst (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/59281#comment_123675

What is Fzerspan? And in your latest comment, cuttingforce = 0 if the condition is met but at the start you set it to 1. So I wasn't quite sure what to do in my vectorized answer below.