How to add specific rows that meet a condition?

4 ビュー (過去 30 日間)
Thomas
Thomas 2016 年 4 月 15 日
編集済み: Roger Stafford 2016 年 4 月 16 日
Hi,
I have two column vectors dim 98x1 one is the unique values of an array (A) and the other is the percentage that these values occur (B). What I am trying to get is to sum the percentile values that are between 0-1 of the unique values and then 1-2 and so on. So that I have the percentile values of occurrence for everything =<1, =<2, ..., =<26 Here is an example of what I am trying to do,
%
A = [0.1 0.5 1.0 1.2 1.6 2.0]' % The unique values
B = [50.3 3.0 2.5 2.4 1.8 1.4]' % The percentile values
C = [55.8 5.6] % The percentile values that are in the range specified
So far I have been working with the following loop
%
for i = 1:size(A,1)
for j = 0:25
if A <= i & A >= j
C(i,j) = sum(B(j,i);
end
end
end
Not entirely sure I I am going the right way about this. If anyone has any advice it would be greatly appreciated.

回答 (2 件)

Wayne King
Wayne King 2016 年 4 月 15 日
How about just
[sum(B(A<=1)) sum(B(A>1 & A<=2))]
  1 件のコメント
Thomas
Thomas 2016 年 4 月 15 日
Thanks this works a treat, however, I am trying to incorporate it into a for loop to complete multiple iterations but I'm having difficulty getting it to move to the next column. I've put it in like this,
%
for i = 1:26
for j = 2:27
C = [sum(B(A<=1)) sum(B(A>i & A<=j))]
end
end

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


Roger Stafford
Roger Stafford 2016 年 4 月 15 日
編集済み: Roger Stafford 2016 年 4 月 15 日
N = 1:26;
C =sum(bsxfun(@times,bsxfun(@le,A,N),B));
  2 件のコメント
Thomas
Thomas 2016 年 4 月 15 日
This adds the first values together in the range of 0-1 but for 1-2 it adds the first set of values. Is there anyway to separate between each range?
Roger Stafford
Roger Stafford 2016 年 4 月 16 日
編集済み: Roger Stafford 2016 年 4 月 16 日
@Thomas: My apologies. Forget that solution I gave you. My head was not on straight this morning. There is a valid method using 'histc' and 'accumarray', but you might as well use obvious methods with for-loops for this unless your data is very large. (Let me know if you want to know the 'histc-accumarray' method.)

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by