Apply function on array

2 views (last 30 days)
x x
x x on 20 Sep 2019
Answered: Star Strider on 20 Sep 2019
I would like to create function which will take number from the second position in array, add numbers from the first and third position in that array and write down as a second position in the output array. So the formla would be F2=A1+A2+A3 if A is our input array and F is output. It should work for unknow lenght of array and take 0 for first and last number (so for F1= 0+A1+A2). I would be very thankful for any advice how I should do it...

Answers (2)

madhan ravi
madhan ravi on 20 Sep 2019
Probably you're looking for cumsum()
doc cumsum

Star Strider
Star Strider on 20 Sep 2019
Try this:
A = randi(9, 10, 1) % Create ‘A’
F = zeros(size(A)); % Preallocate ‘F’
F1 = filter([1 1 1], 1, A); % All Releases
F2 = movsum(A, [1 1]); % In R2016a & Later
F(2:end-1) = F1(3:end) % Desired Output
F(2:end-1) = F2(2:end-1) % Desired Output
Experiment to get the result you want

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!