フィルターのクリア

How to find cell position in a column based on sum of column.

5 ビュー (過去 30 日間)
Parthu P
Parthu P 2019 年 11 月 21 日
コメント済み: Parthu P 2019 年 11 月 21 日
Hi,
I have a 365x500 matrix (A). How to calculate another matrix B (3x500) such that each row of matrix B contains:
row1: sum of each column (I did this)
row2: 50% of sum of each column (ie. 0.5*values in row1) (I did this)
row3: positions of the cells in each column of matrix (A) such that cumulative sums of all the previous cells in each column of A is less than or equal to values in row2 (50% of sum of each column).
Thanks in advance.

採用された回答

Adam Danz
Adam Danz 2019 年 11 月 21 日
編集済み: Adam Danz 2019 年 11 月 21 日
B(1,:) = sum(A);
B(2,:) = B(1,:)/2;
B(3,:) = sum(cumsum(A)<=B(2,:));
% sanity check
% Checks that the cumulative sums of each column in A up until the
% row numbers identified in B(3,:) are <= B(2,:) and that the
% cumulative sum of each column in A up until the rows B(3,:)+1 exceed
% the values in B(2,:)
Ac = cumsum(A);
allTrue = all(Ac(sub2ind(size(A),B(3,:),1:size(A,2))) <= B(2,:));
allFalse = ~any(Ac(sub2ind(size(A),B(3,:)+1,1:size(A,2))) <= B(2,:));
if ~allTrue || ~allFalse
error('Something''s wrong.')
end
  1 件のコメント
Parthu P
Parthu P 2019 年 11 月 21 日
Thanks a lot!
Perfect answer.

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

その他の回答 (1 件)

Ridwan Alam
Ridwan Alam 2019 年 11 月 21 日
A = rand(365,500);
B = zeros(3,500);
B(1,:) = sum(A);
B(2,:) = sum(A)/2;
B(3,:) = sum((B(2,:)-cumsum(A))>0);

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by