I have a 26x26 matrix 'A' of type double. I want to sum all of its elements using S = sum(A, 'all').
I keep getting the following error:
Error using sum
Invalid option. Option must be 'double', 'native', 'default', 'omitnan' or 'includenan'.
Can someone help me with this? My matrix is of the correct type, so I am not sure what's wrong.

12 件のコメント

Awais Ali
Awais Ali 2018 年 12 月 26 日
use S=sum(sum(matrix))
Steven Lord
Steven Lord 2018 年 12 月 26 日
Please show us the output of the version function. I agree with the other posters that you probably are using a release prior to the introduction of that option.
Seangleng Khe
Seangleng Khe 2023 年 11 月 24 日
編集済み: Seangleng Khe 2023 年 11 月 24 日
I had this problem too. I cannot use this command. Yet I use sum(sum(c)). It will give you the same result.
Walter Roberson
Walter Roberson 2023 年 11 月 24 日
@Seangleng Khe which MATLAB release are you using, and what is class(c) ?
Seangleng Khe
Seangleng Khe 2023 年 11 月 24 日
@Walter Roberson What I mean is that I face this problem too. So instead of sum(A,'all'), which does not work for me, I use sum(sum(A)). There is nothing relating to class(c).
Stephen23
Stephen23 2023 年 11 月 24 日
"It will give you the same result."
No, not in general:
format short G
A = rand(4,3,2);
sum(sum(A))
ans =
ans(:,:,1) = 4.1374 ans(:,:,2) = 4.1502
sum(A,'all')
ans =
8.2876
sum(A(:))
ans =
8.2876
Walter Roberson
Walter Roberson 2023 年 11 月 24 日
Please show us the output of the following command:
version
Paul
Paul 2023 年 11 月 24 日
sum(sum(X)) is not guaranteed to give the exact same result as sum(X,'all'), even for two-dimensional X
rng(101)
X = rand(100);
isequal(sum(X,'all'),sum(sum(X)))
ans = logical
0
These are equal for this case, but is it guaranteed for all X?
isequal(sum(X,'all'),sum(X(:)))
ans = logical
1
DGM
DGM 2023 年 11 月 25 日
編集済み: DGM 2023 年 11 月 25 日
To extend this to what Walter said about class:
% i'm avoiding rand() hopefully avoid any version/platform
% sensitivity in generating the test array, mostly because i wanted
% to run the test in versions prior to the introduction of rng()
X = magic(1000)/3; % X is double
SA = sum(X,'all');
abs(SA - sum(sum(X))) % error scales with data
ans = 3.0518e-05
abs(SA - sum(X(:))) % equal
ans = 0
X = single(X); % X is single
SA = sum(X,'all');
abs(SA - sum(sum(X))) % error is not insignificant
ans = single 278528
abs(SA - sum(X(:))) % still equal
ans = single 0
So for a 2D input, sum(sum(X)) does not give the same result, and the difference between the methods does depend on the array class, and it also depends on the version (and maybe platform) as well. I get different results on every version I've tested. But in all cases, sum(X,'all') and sum(X(:)) yield identical results.
We can question whether it's prudent to blindly try taking the sum of large single-precision arrays, but that's not really the point.
Bruno Luong
Bruno Luong 2023 年 11 月 25 日
編集済み: Bruno Luong 2023 年 11 月 25 日
The sum is never guaranteed to be indentical even for the same input, same (apparent) command, same PC, same version
X=rand(1,1e6);
n=maxNumCompThreads(1); s1=sum(X,'all');
maxNumCompThreads(n); sn=sum(X,'all');
isequal(s1,sn)
ans = logical
0
s1-sn
ans = -2.3283e-10
DGM
DGM 2023 年 11 月 25 日
Ah.
Paul
Paul 2023 年 11 月 25 日
編集済み: Bruno Luong 2023 年 11 月 25 日
I assume sum(X,'all') is implemented as sum(X(:)) at the intermediate interface level before the sum hoerachy and low-level CPU instruction occurs, so I think yes, they should return the same result.

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

 採用された回答

madhan ravi
madhan ravi 2018 年 12 月 26 日
編集済み: madhan ravi 2018 年 12 月 26 日

1 投票

sum(A(:)) % if it doesn't work either upload your matrix as .mat file

9 件のコメント

Nour Aburaed
Nour Aburaed 2018 年 12 月 26 日
This is one way around it, but I still would like to know the reason for that error
madhan ravi
madhan ravi 2018 年 12 月 26 日
編集済み: madhan ravi 2018 年 12 月 26 日
Edited:
Maybe because your using an older version of Matlab , I believe 'all' (vector dimension argument) was released in 2018b. See https://www.mathworks.com/help/matlab/release-notes.html?rntext=%22vector+dimension%22&startrelease=R2015aSP1&endrelease=R2018b&groupby=release&sortby=descending&searchHighlight=%22vector+dimension%22 . What release are you using ? upload your matrix as .mat file to experiment (to see if I can reproduce that error) .
Nour Aburaed
Nour Aburaed 2018 年 12 月 26 日
編集済み: Nour Aburaed 2018 年 12 月 26 日
I am using the latest release
Stephen23
Stephen23 2018 年 12 月 26 日
編集済み: Stephen23 2018 年 12 月 26 日
"I believe 'all' name value pair... "
It is not a "name-value" pair, it is simply an optional input argument. For example, see textscan for examples of optional inputs which must be given as name-value pairs:
sum(...,'all') % not a name-value pair.
textscan(...,'Delimiter',';')
% ^^^^^^^^^ name
% ^^^ value
John D'Errico
John D'Errico 2018 年 12 月 26 日
編集済み: John D'Errico 2018 年 12 月 26 日
Actually, you cannot be using the newest release. Currently, the newest release is R2018b.
This capability:
S = sum(X,'all') sums all elements of X.
was introduced in R2018b. It did not exist in R2018a. (I have both versions active on my computer. So I verified that fact.)
You may think you are using that release, but if sum fails, producing the error described, then you are using an older release.
For example, in R2018b, you can do this:
A = rand(26,26);
sum(A,'all')
ans =
336.422314041605
That will fail in any older release.
You can check what release you are running by simply using ver. At the command line in MATLAB, type
ver
In R2018b, you should see something like:
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.5.0.944444 (R2018b)
MATLAB License Number: ********
...
madhan ravi
madhan ravi 2018 年 12 月 26 日
編集済み: Image Analyst 2018 年 12 月 26 日
Thank you very much John D'Errico appreciate it.
@Nour respond to John D'Errico's comment and let us know..
Image Analyst
Image Analyst 2018 年 12 月 26 日
It's possible he's using the latest student release. I heard that in most years there is no "b" student release, just an "a" release. But that wouldn't have 'all' in it, however (:) will always work.
madhan ravi
madhan ravi 2018 年 12 月 26 日
But sir Image Analyst I’m using student version too , it works in my case though.
Steven Lord
Steven Lord 2018 年 12 月 26 日
Image Analyst: clicking on the "Buy MATLAB and Simulink Student Suite" button on this page brings up the store page for the Student Version of release R2018b.

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

その他の回答 (0 件)

カテゴリ

製品

質問済み:

2018 年 12 月 26 日

編集済み:

2023 年 11 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by