Very tricky:Manipulation with complex array
2 次查看(过去 30 天)
显示 更早的评论
I need to write a m.file function for this.
For example
array X = [4, 1, 1, 3, 8, 12], I need to make the array which will contain bold ones as well Y = [4, 1, 5, 1, 3, 4, 8, 12, 20].
采纳的回答
Fabio Freschi
2022-9-19
编辑:Fabio Freschi
2022-9-19
% original vector
X = [4, 1, 1, 3, 8, 12];
% reshape to have 2xN vector
W = reshape(X,2,[]);
% calculate the sum
S = sum(W,1);
% concat the vector and reshape
Y = reshape([W; S],1,[])
3 个评论
Fabio Freschi
2022-9-19
Why using for loops when the vectorized version is effective, cleaner and more efficient?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!