Sum of empty matrix

조회 수: 2 (최근 30일)
Thomas
Thomas 2012년 3월 30일
The sum of an empty matrix does not always follow the rule 'sum(empty) = 0'. What's up with this?
>> sum(ones(1,0))
ans = 0
>> sum(ones(0,1),2)
ans = Empty matrix: 0-by-1
>> sum(ones(2,0))
ans = Empty matrix: 1-by-0
>> sum(ones(0,2),2)
ans = Empty matrix: 0-by-1
  댓글 수: 1
Thomas
Thomas 2012년 3월 30일
There is also some funny business with find on an 'all false' matrix, the behavior of find(0) seems inconsistent.
>> [x y] = meshgrid(0:2,0:2)
x =
0 1 2
0 1 2
0 1 2
y =
0 0 0
1 1 1
2 2 2
>> x=num2cell(x)
x =
[0] [1] [2]
[0] [1] [2]
[0] [1] [2]
>> y = num2cell(y)
y =
[0] [0] [0]
[1] [1] [1]
[2] [2] [2]
>> cellfun(@(z,w) find(zeros(z,w)), y, x, 'UniformOutput', false)
ans =
[] [0x1 double] [0x1 double]
[1x0 double] [] [1x0 double]
[0x1 double] [0x1 double] [0x1 double]

댓글을 달려면 로그인하십시오.

답변 (2개)

the cyclist
the cyclist 2012년 3월 30일
I don't think this answers your question, but it does give some related info: http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/

Jan
Jan 2012년 3월 30일
Especially:
sum(ones(1, 0)) % 0
sum(ones(2, 0)) % Empty matrix: 1-by-0
is not intuitive anymore. Please contact the technical support and ask for an enhancement.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by