Apply function on array

조회 수: 1 (최근 30일)
x x
x x 2019년 9월 20일
답변: Star Strider 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