Because val1 is not preallocated in the code you've showned, it isn't obvious that is created by the loop. val1 is shorter by 1 element than val because you explicitly construct it so. Your loop is
for loopindex 2 : numel(val)-1
val1(loopindex) = ...
end
If you want val1 to be the same length as val then you can assign 0 to the last element, the same way 0 is automatically assigned to val1(0) at the first step of the loop when it does val(2) = .... So after the loop you could have:
But the loop is completely unneeded and is a very slow way to construct val1 (particularly without the aforementioned preallocation). This would achieve the same, including creating val1 the correct length: valdiff = diff(val);
val1 = [false, valdiff(1:end-1) > 0 & valdiff(2:end) < 0 & val(2:end-1) > 4, false];
With that fix your stem line will work.
No idea why you tried to use bsxfun or why you mentioned it. There is nowhere in your code where it could be used.
Best Answer