summations and selections of the columns of a matrix

1 回表示 (過去 30 日間)
ELISABETTA BILLOTTA
ELISABETTA BILLOTTA 2021 年 10 月 28 日
コメント済み: ELISABETTA BILLOTTA 2021 年 10 月 28 日
I have a matrix of this type
10 15 37 16
6 4 3 5
15 66 85 96
17 89 15 82
58 66 78 100
I have to understand how to do two procedures:
1) calculate the sums of the subsequent column numbers, for example :
the first line remains the same 10 15 37 16
the second line must be 6+15+17+58 4+66+89+66 3+85+15+78 5+96+82+100
the third line must be 5+17+58 66+89+66 85+12+78 6+82+100
and so on ... can someone tell me how it can be done?
2) in the script then I have to consider the last one formed by as the first column
16
5
96
85
100
as the second column
37
3
85
15
78
and so on .. how do you select any column of the matrix you can use?
  2 件のコメント
Johnny Himbele
Johnny Himbele 2021 年 10 月 28 日
Hi Elisabetta,
A cumsum function can achieve what you're trying to do. For more information, please refer to this link.
For example,
A = magic(5);
B = [A(1,:); cumsum(A(2:end,:),'reverse')];
If you want to select any specific columns, please replace a colon to any column indices.
% First column
C = [A(1,1); cumsum(A(2:end,1),'reverse')];
% Second and third columns
D = [A(1,2:3); cumsum(A(2:end,2:3),'reverse')];
Johnny
ELISABETTA BILLOTTA
ELISABETTA BILLOTTA 2021 年 10 月 28 日
perfect thank you very much!!!!

サインインしてコメントする。

採用された回答

David Hill
David Hill 2021 年 10 月 28 日
newMatrix=[yourMatrix(1,:);flipud(cumsum(flipud(yourMatrix(2:end,:))))];
  1 件のコメント
ELISABETTA BILLOTTA
ELISABETTA BILLOTTA 2021 年 10 月 28 日
ok thanks ... I understand that is the answer to point 1 and it works .. thanks a lot .. and for point two?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLanguage Fundamentals についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by