The role of singleton elements in calls to the zeros function.

조회 수: 2 (최근 30일)
Marshall
Marshall 2014년 10월 2일
댓글: Marshall 2014년 10월 2일
Hello MatLab'rs,
I'm confused by the return of zeros under certain circumstances.
For some vector V, calling zeros will create a length(V) dimensional matrix:
>> V = [ 1:10 ]
V =
1 2 3 4 5 6 7 8 9 10
>> Z = zeros(V); % Z = <10-D double>
In my code I have a selectedDimLengths vector, each element of which can change from a singleton up to about 6 or 7, but often looks something like:
selectedDimLengths =
1 1 1 5 5 1 1 1 3 1 1 1 1
>> Z = zeros(selectedDimLengths); % Z = <9-D double>
when I pass this to zeros I lose a couple of dimensions, down to 9 when I expect 13. What role do the singleton elements play here? Why?
Are the trailing singleton elements cropped somehow?

채택된 답변

Marshall
Marshall 2014년 10월 2일
Um, so I'll answer my own question instead of deleting in case someone comes along looking.
I found the information required here in this article by the amazing Loren Shure:
"Arrays in MATLAB are N-dimensional, with an infinite number of trailing singleton dimensions. Trailing singleton dimensions past the second are not displayed or reported on, e.g., with size. "
So I just now need to think of a different way to assess that vector... :)
  댓글 수: 1
Marshall
Marshall 2014년 10월 2일
Oh, typed and went to lunch, submitted and then found Iain's answer too.

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

추가 답변 (1개)

Iain
Iain 2014년 10월 2일
Matlab only really needs to know which dimensions are "nonsingular". - A scalar has infinite dimensions, all with a size of 1. - A vector has infinite dimensions, only one of which has a size other than 1.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by