Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

How do I do calculations with the last and first array member in a for loop?

1 Ansicht (letzte 30 Tage)
abraham rodriguez
abraham rodriguez am 5 Sep. 2018
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
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?

Antworten (1)

the cyclist
the cyclist am 5 Sep. 2018
Bearbeitet: the cyclist am 5 Sep. 2018
test = [1 2 3];
N = numel(test);
idx = [1:N; [2:N,1]];
consecutiveSum = sum(test(idx));

Diese Frage ist geschlossen.

Produkte


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by