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].
댓글 수: 3
채택된 답변
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!