Can anyone please clarify "what's the difference when a matrix is declared as zerosfrom ones"?
이전 댓글 표시
for eg:
arr=zeros(row,col); #create a matrix of size row X col with all elements #initialized to zero #similarly arr1=ones(row,col); #do the same except the initialized elements will be all 1's.
My doubt is why a matrix need to be declared zeros for computation??? Or Whats the basic idea behind initializing to all zeros??
답변 (2개)
Honglei Chen
2013년 3월 14일
0 개 추천
zeros is recommended to be used for preallocation purpose. Below is the link to the documentation. See the section Preallocating Arrays
댓글 수: 4
soumya
2013년 3월 27일
Honglei Chen
2013년 3월 27일
I'm purely guessing here but I think it has to do with lower level implementations. My guess is that zeros simply allocate the memory, sort of like "static variable" in C, but to use ones, there is an extra step of assignment.
Image Analyst
2013년 3월 27일
I think they both have to be assigned, otherwise the memory would just be whatever value they had last.
Walter Roberson
2013년 3월 27일
Memory initialized to 0 can, in some cases, be initialized by hardware "demand zero paging", in which a memory page is zeroed at the hardware level instead of having to write zeroes to it.
Walter Roberson
2013년 3월 27일
편집: Walter Roberson
2013년 3월 27일
0 개 추천
- it is common in problems for a result of 0 not to be possible but 1 to be possible, so 0 is more likely "available" to act as a marker that an element is not filled yet
- 0 is the only numeric value that is logical "false" but all non-zero values are logical "true", making writing logical tests easier
- 0 is the same in integer and floating point representation
- 0 is the "no element here" marker for sparse arrays
- 0 is the identity element for cumulative summing, which turns out to be more common than cumulative multiplications
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!