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

1 次查看(过去 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 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by