Apply function on array

1 次查看(过去 30 天)
x x
x x 2019-9-20
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...

回答(2 个)

madhan ravi
madhan ravi 2019-9-20
Probably you're looking for cumsum()
doc cumsum

Star Strider
Star Strider 2019-9-20
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

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by