Adds one element in vector from another, by position in array
14 次查看(过去 30 天)
显示 更早的评论
Say I have a vector 'x' that is [1:100], and I want to create a function that creates a new vector 'y' that is the sum of x(n+2)+x(n+3) at each position (n) of x (beginning at 1 and moving through the vector). What would this function look like?
This is my attempt at the function, but it does not work
for i=1:size(x)
y(i) = [x(i+2)+x(i+3)]
...
end
Thank you for any help!
0 个评论
采纳的回答
Stephen23
2020-12-23
x = 1:100; % superfluous square brackets removed.
y = x(3:end-1)+x(4:end) % the MATLAB way.
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!