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 个)

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)

类别

帮助中心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!

Translated by