Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
How do I do calculations with the last and first array member in a for loop?
조회 수: 2 (최근 30일)
이전 댓글 표시
for example, I want to show another matrix that its elements are the sum of consecutive members in the original array, and the last showing the sum of the last with the first element, without explicitly managing the last case.
test = [1 2 3];
consecutiveSum = [ test(1) + test(2) , test(2) + test(3) , test(3) + test(1) ];
How can I implement this in a for loop?
댓글 수: 0
답변 (1개)
the cyclist
2018년 9월 5일
편집: the cyclist
2018년 9월 5일
test = [1 2 3];
N = numel(test);
idx = [1:N; [2:N,1]];
consecutiveSum = sum(test(idx));
댓글 수: 0
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!