Why does accumarray require vals and subs to be of the same size?

1 回表示 (過去 30 日間)
Jonathan
Jonathan 2014 年 6 月 23 日
コメント済み: Cedric 2014 年 6 月 23 日
Hi,
I understand this is a built-in function and that "legacy constraints" might be the answer, but still I'm really wondering why the inputs `subs` and `vals` need to be of the same size in `accumarray`.
IMO, a sufficient input check on subs (forgetting about input cells..) would be something like
subs = floor(subs);
assert( all(subs>0) && all(subs <= numel(vals)), 'Index out ouf bounds.' );
I really don't understand this constraint, can someone explain the logic behind this?

採用された回答

Jan
Jan 2014 年 6 月 23 日
The documentation of accumarray is one of the most complicated instructions I've read. It cannot compete with the cute clarity of the other parts of Matlab's excellent docs.
The job of accumarray is "accumulating elements of the vector val using the elements of subs as indices". Therefore the number of values and the corresponding indices must be equal. The i.th value is accumulated accoring to the i.th subs element.
  3 件のコメント
Jonathan
Jonathan 2014 年 6 月 23 日
My mistake sorry, I just understood your last sentence and Matlab's documentation... It's all clear now. Thanks :)
Cedric
Cedric 2014 年 6 月 23 日
Yep, understand each element of subs as the "address" where to stack the corresponding element of vals before applying the accumulation function.

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by