Inappropriate MATLAB warning when summing over columns

16 ビュー (過去 30 日間)
Kaushik Lakshminarasimhan
Kaushik Lakshminarasimhan 2017 年 11 月 5 日
Suppose X is an Nx10 matrix and I want to sum over each of the 10 columns, regardless of number of rows. I use:
Y = sum(X);
However, N can sometimes be equal to 1 in which case X only has one row. The above operation will now sum over the elements of that row. To avoid this, I change my operation to:
Y = sum(X',2)
which works for any generic N. Now when I hover my cursor over "sum", I see the following warning: Instead of using transpose ('), consider using a different DIMENSION input argument to SUM. I find this warning misleading because clearly the alternative won't work. Is there an alternative to using transpose in my case?
  2 件のコメント
Stephen23
Stephen23 2017 年 11 月 5 日
"I find this warning misleading because clearly the alternative won't work."
Really, why won't this work?:
sum(X,1)
Kaushik Lakshminarasimhan
Kaushik Lakshminarasimhan 2017 年 11 月 5 日
It worked. I should've tried before asking.

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

採用された回答

Guillaume
Guillaume 2017 年 11 月 5 日
The warning is entirely accurate, if you want sum(X) to sum across the rows whether or not X is a vector (i.e. sum will always return a vector of size 1 x size(X, 2), then you should use
Y = sum(X, 1);
Your Y = sum(X', 2) is also the same as Y = sum(X, 1)' and is never equivalent to your Y=sum(X) (it's the conjugate transpose for non-vectors).

その他の回答 (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