reduce the length by summing the bins

6 ビュー (過去 30 日間)
Junaid
Junaid 2012 年 6 月 24 日
コメント済み: Tamara del Águila 2021 年 5 月 23 日
dear all,
Let say I have matrix of Histograms, where each column is one histogram vector. What I want is to sum two bins each other, i.e., first bin + second bin, Third bin + forth bin.
Let say
A =
[ 1 2 3
4 5 6
7 8 9
10 11 12]
output should be
B =
[ 5 7 9
17 19 21 ]
Is it possible without using loops.

採用された回答

Walter Roberson
Walter Roberson 2012 年 6 月 24 日
B = A(1:2:end, :) + A(2:2:end,:);
  1 件のコメント
Junaid
Junaid 2012 年 6 月 24 日
Thank you.

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

その他の回答 (1 件)

Jan
Jan 2012 年 6 月 24 日
Faster for large arrays, because it does not create temporary arrays, e.g. 3 times faster for [1e5 x 3] in Matlab 2009a/64/Win:
B = reshape(sum(reshape(A, 2, 2, 3)), 2, 3);
[EDITED] General approach:
newLen = size(A, 1) / 2;
B = reshape(sum(reshape(A, 2, newLen, 3)), newLen, 3);
See also: FEX: BlockMean
  5 件のコメント
Tamara del Águila
Tamara del Águila 2021 年 5 月 21 日
... and how could I possibly downsample an array binning the data? With resample is not possible because it calculate averages, so I was wondering if it would be possible to do it with something like this answer, so that the matrix can be resize (let's say, to the half or less) without loosing the counts.
Tamara del Águila
Tamara del Águila 2021 年 5 月 23 日
About what I further commented on the topic, In any case anyone is interested, has the same problem, or would like to share its knowledge about the specific issue, I have posted a formal question, because I am stuck with it...
thanks!

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

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by