Building a vector array in for loop from if statements

I'm working on an assignment requiring a function to build a vector containing all of the factors of an input value. I have the algorithm to find the factors, but how do I get them to hold in a vector as the loop runs?
function factors = compute_factors(f)
%COMPUTE_FACTORS(N) Generates array of all factors of a input f.
% Evaluates if input f is a scalar and a positive whole number using logical
% arguments, then generates a vector of all the factors of f.
if f<0
error('f must be a postive whole number');
elseif isscalar(f) ~= 1
error('f must be a scalar')
end
for vec = 1:f
if rem(f,vec)==0
factors = [vec]
end
end
I know that I need to use the zeros function to generate a vector, but if I don't know how many inputs there will be without running the loop, how can I define it beforehand? Any help is greatly appreciated!

 채택된 답변

David Fletcher
David Fletcher 2018년 4월 8일
You can build the output array dynamically. This is generally frowned upon, but unless the arrays are large it's unlikely to make any noticeable difference to the speed of your code.
function factors = compute_factors(f)
%COMPUTE_FACTORS(N) Generates array of all factors of a input f.
% Evaluates if input f is a scalar and a positive whole number using logical
% arguments, then generates a vector of all the factors of f.
if f<0
error('f must be a postive whole number');
elseif isscalar(f) ~= 1
error('f must be a scalar')
end
factors=[];
for vec = 1:f
if rem(f,vec)==0
factors = [factors vec]
end
end

댓글 수: 4

The support through the MATLAB community is incredible! Thank you so much for the prompt response. Since you mention that dynamically building the vector is frowned upon, is there a more efficient approach that you would recommend? Thanks again!
The alternative is to preallocate, but since you don't really know how much space you'd need, I suppose the strategy would be to preallocate a block much larger than you're likely to need and then trim it down afterwards. Though personally, unless the code is running really slow, I personally wouldn't bother myself about it. People can frown all they like: it makes no odds to me.
For the sake of "scientific curiosity", I added the tic/toc operators around the for loop and let it crunch a 12 digit input... It's been at it for a while, but I'll let you know when it's done ;)
David Fletcher
David Fletcher 2018년 4월 8일
편집: David Fletcher 2018년 4월 8일
You piqued my curiosity. I just tried it with an 11 digit number (both with and without preallocation) and the interesting thing is that both routines took the same amount of time (Just over 158 seconds). Seems that Matlab probably already has some internal optimization that it applies when dynamically growing arrays. Though, in this case the arrays involved are fairly small - it would probably be a bit different if the arrays became large

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2018년 4월 8일

편집:

2018년 4월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by