필터 지우기
필터 지우기

uses of 'zeros' and 'ones' function ?

조회 수: 77 (최근 30일)
yogesh jain
yogesh jain 2015년 11월 6일
댓글: Steven Lord 2020년 9월 13일
hello all, what are the main uses of functions 'zeros' and 'ones' ? Why we use them ? thank you

채택된 답변

James Tursa
James Tursa 2015년 11월 6일
A couple of examples.
zeros is often used to allocate a variable to a known size, then downstream code will fill in the individual elements. E.g.,
x = zeros(1,5); % Pre-allocate x to the correct size
for k=1:5
x(k) = k^2; % Fill in the individual elements
end
ones could be used to allocate a variable with a constant. E.g.,
x = ones(1,5) * 7; % Allocate a variable and fill it with 7's
  댓글 수: 3
Marcus Sandquist Lignell
Marcus Sandquist Lignell 2015년 11월 6일
In addition to this, when using for loops; pre-allocating a matrix in the correct size for the output results will speed up your solve time.
yogesh jain
yogesh jain 2015년 11월 6일
Thank you very much all (Mr. James Tursa ,Stephen Cobeldick and Marcus Sandquist Lignell) . That was pretty useful :)

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

추가 답변 (1개)

Dimithra Anton
Dimithra Anton 2020년 9월 13일
편집: Dimithra Anton 2020년 9월 13일
zeros(length(numbers),2)
what about this??? what does (numbers) do and why is the number of columns 6?
  댓글 수: 1
Steven Lord
Steven Lord 2020년 9월 13일
numbers is an array that is being passed into the length function (or it is a function that can be called with no input arguments and whose first output is being passed into the length function.)
I would not expect that command to return an array with six columns. Assuming you haven't written or downloaded a zeros.m function that is taking precedence over the built-in function it should return a matrix with two columns.
which -all zeros

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by