Can anyone please clarify "what's the difference when a matrix is declared as zerosfrom ones"?
Mostrar comentarios más antiguos
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??
Respuestas (2)
Honglei Chen
el 14 de Mzo. de 2013
0 votos
zeros is recommended to be used for preallocation purpose. Below is the link to the documentation. See the section Preallocating Arrays
4 comentarios
soumya
el 27 de Mzo. de 2013
Honglei Chen
el 27 de Mzo. de 2013
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
el 27 de Mzo. de 2013
I think they both have to be assigned, otherwise the memory would just be whatever value they had last.
Walter Roberson
el 27 de Mzo. de 2013
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
el 27 de Mzo. de 2013
Editada: Walter Roberson
el 27 de Mzo. de 2013
0 votos
- 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
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!