For loop only running once.
显示 更早的评论
Hi, I'm new with MATLAB and I am attempting run a for loop which will tell me how many times a certain set of digits are divisible by ANY of 6 given numbers. Essentially, I am trying to see how many times single digit numbers (1-9) are divisible by 3, 5, 7, 11, OR 13. Here's my script, but the problem is that it only runs the first loop for X=3
s1=0 x=0:9; for X=3;5;7;11;13; s1=(rem(x,X)==0); end disp(s1)
回答(2 个)
per isakson
2012-2-7
This code does what I believe you want it to do
x=0:9;
s1 = nan(5,10); % allocate memory
ii = 0;
for X = [3,5,7,11,13] % should be a row vector
ii = ii + 1;
s1(ii,:) = (rem(x,X)==0);
end
disp(s1)
Andrei Bobrov
2012-2-7
s1 = bsxfun(@(x,y)rem(y,x)==0,[3,5,7,11,13]',0:9);
类别
在 帮助中心 和 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!