The name of your function generatezeros is very misleading since what that function does is find the first 3 and the last indices where a vector changes sign. It does not generate anything in any way. Considering that there is no comment in your code, at least use a function name that explains what it actually does.
Note that
is the same as
it is not equivalent to
This else wil be executed for all k greater than 3, each time setting o to the index i. So at the end of the loop o will be the index of the last sign change in the vector. If you meant o to be the 4th one, then that's not what your code does.
In any case, the whole function can be simplify to:
function locations = findzerocross(y)
locations = find(diff(sign(y)) ~= 0);
end
If you want the first four locations, you can then index the returned array
y = coder.load('C:\Users\HP\Desktop\Project Simulation\water\WaterBonePlaneInterface\SimulationResults\B.mat');
zerocrossing = findzerocross(y);
zerocross4 = zerocrossing(1:4);
I know nothing about coder, so no idea what you need to do to make it compile.