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!
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!