필터 지우기
필터 지우기

Adding two arrays of different sizes together evenly without messing cumulative sum.

조회 수: 12 (최근 30일)
I have 2 arrays.
A = [0 1 2 3 4 5 6 . . . . n] % Size 2402 x 1, assume in this case n number is 58
B = [18] % Size 1x1
C = [Cumulative output of A+B] % expected result starting from 0 and ending at 76 @ size of 2402 *1
Now
A is a cummulative sum of energy consumed starting from 0 and ending at 58 kWh during 2402 seconds.
B is total auxiliary energy consumed during 2402 seconds. Please note B is not a cumulative energy. Its just a number in 1 x 1 array.
What I am trying to do is to add B to A evenly such that the result C can be a cumulative result starting from 0 and ending at 76.
I tried a lot of logics but nothing worked for me.
Kindly guide me in correct direction to acheive this result.

채택된 답변

Akira Agata
Akira Agata 2019년 10월 24일
Based on the question, C should be a cumulative result starting from 0. So, it should be:
C = A + linspace(0,B,length(A));
  댓글 수: 2
Shahab Khan
Shahab Khan 2019년 10월 24일
Hi, Thanks its works. However, the C array is in the form of 2402 x 2402. I actually need it in the form of 2402 x 1 or 1 x 2402.
Shahab Khan
Shahab Khan 2019년 10월 24일
편집: Shahab Khan 2019년 10월 24일
Ok I managed to do it myself in this maner.
First I tried to understand linespace function. When I understood how it works. I write this code to acheive the above goal.
A = [0 1 2 3 4 . . . . n]; % Size 2402 x 1
B = [18];
D = linspace(0,B,length(A));
E = D';
C = A + E; % Size 2402 x 1
Thanks for the help.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2019년 10월 24일
편집: Walter Roberson 2019년 10월 24일
C = cumsum(A + B / length(A));

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by