Apply function on array

1 Ansicht (letzte 30 Tage)
x x
x x am 20 Sep. 2019
Beantwortet: Star Strider am 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...

Antworten (2)

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

Star Strider
Star Strider am 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

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by