Error using sum function
이전 댓글 표시
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
2018년 12월 26일
use S=sum(sum(matrix))
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
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
2023년 11월 24일
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).
"It will give you the same result."
No, not in general:
format short G
A = rand(4,3,2);
sum(sum(A))
sum(A,'all')
sum(A(:))
Walter Roberson
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)))
These are equal for this case, but is it guaranteed for all X?
isequal(sum(X,'all'),sum(X(:)))
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
abs(SA - sum(X(:))) % equal
X = single(X); % X is single
SA = sum(X,'all');
abs(SA - sum(sum(X))) % error is not insignificant
abs(SA - sum(X(:))) % still equal
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.
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)
s1-sn
DGM
2023년 11월 25일
Ah.
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.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!