converting Excel formula to Matlab (adding two arrays different lengths)

조회 수: 1 (최근 30일)
Engineer Undergoing
Engineer Undergoing 2021년 7월 30일
답변: dpb 2021년 7월 30일
Hello everyone,
I am trying to convert this formula from excel to matlab but I couldn't as my attempts faild and I am no expert in matlab.
this is the Excel formula: the value of C4: =C3+B4 and continues to the end of the C column (450x1):
Note the first value on the C column has to be a zero, hence that's why i used zeros
my attempt on matlab:
EData = zeros(19857,1);
Data = idx + EData(1:end);
EData beign the C column and idx the B column

채택된 답변

dpb
dpb 2021년 7월 30일
C=[0;cumsum(B)];
where B is your variable in column B
MATLAB uses vector operations; to append a vector to a single value (or even another vector) just once, the explicit catenation shorthand using [] and ; is fast enough to not bother with the preallocation and explicit addressing of indices within the preallocated output array.
If one were doing this inside a for...end loop of significant size, then the preallocation step would be needed to prevent rapid performance degradation by the need for dynamic allocation and reallocation.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by