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

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Eric Sampson
on 2 Apr 2013

This is probably easy, but my brain isn't working today...

How can you do the following operation in a vectorized way? I'd think it should be possible with some combination of cumsum, diff & logical indexing:

input = rand(10,1); output = zeros(size(input); output(1) = input(1); for ind = 2:numel(input) dif = input(ind) - input(ind-1); if dif < 0 output(ind) = output(ind-1) + dif; else output(ind) = output(ind-1); end end

Answer by Roger Stafford
on 2 Apr 2013

Accepted answer

Try this.

outp = cumsum([inp(1);min(diff(inp),0)]);

Answer by Matt Tearle
on 2 Apr 2013

There may be better ways, but this works:

d = [true;diff(input)<0]; idx = find(d); output = input(idx(cumsum(d)));

When the array is large enough, there's a pretty decent speedup (~50x)

Show 2 older comments

Eric Sampson
on 2 Apr 2013

Argh, I'm sorry Matt. See my revised problem statement. This was clearly a case of 'do what I mean, not what I say' :)

Sean de Wolski
on 2 Apr 2013

As you know, the DWIM Toolbox still hasn't been released to the public.

## 2 Comments

## the cyclist

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/69558#comment_140730

It would be useful if you also described conceptually what you are trying to do.

## Eric Sampson

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/69558#comment_140734

Basically it's a copy of the input, but anytime that the original increases from one val to the next, the output should be hold constant. Sort of like a copy that can only go down :)