필터 지우기
필터 지우기

how to make cumsum in ascending order of values

조회 수: 3 (최근 30일)
Vishal Sharma
Vishal Sharma 2017년 6월 19일
답변: Image Analyst 2017년 6월 19일
I have a matrix
A = [1 2 4 7;
2 3 1 6;
4 5 6 15]
. The last column is sum of rows
I want to make another column on right side displaying cumulative sums of last column in ascending order of the forth column (row sums). The result of last column shall be as follows:--
[22 28 15]
So, final matrix will become
[1 2 4 7 22;
2 3 1 6 28;
4 5 6 15 15].

채택된 답변

Image Analyst
Image Analyst 2017년 6월 19일
I'm not quite following how your description gives you the result, but I followed your written directions and got this:
A = [1 2 4 7; ...
2 3 1 6; ...
4 5 6 15]
% Sort last column in ascending order:
rightColumn = sort(A(:, end), 'ascend')
% Get the cumulative sums of that
c = cumsum(rightColumn);
% Append onto right edge of A as a new column.
A = [A, c]

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by