필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

matrix with elements sum of elements of old matrix

조회 수: 1 (최근 30일)
MiauMiau
MiauMiau 2015년 5월 7일
마감: MATLAB Answer Bot 2021년 8월 20일
Hi Given I have an array: [3,4,2,5] I want to construct out if it a new array where the two last elements are summed to one - hence: [3,4,7]. Now I want to write code for arrays of aribtrary lengths - how do I do that? I was thinking of a for loop with something like:
for i = 1:length(array)-1
array_new(i) = array(i)
end
array_new(end+1) = array(end)+array(end-1)
but was wondering if there is a more efficient way to do that?

답변 (1개)

Nobel Mondal
Nobel Mondal 2015년 5월 7일
편집: Nobel Mondal 2015년 5월 7일
This would work, but maybe there's a better solution.
a = [3 4 2 5];
a_new = a(1:end-1);
a_new(end) = a_new(end) + a(end);

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by