Apply function on array

2 ビュー (過去 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 Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by