For loop only running once.
Show older comments
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)
Answers (2)
per isakson
on 7 Feb 2012
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
on 7 Feb 2012
s1 = bsxfun(@(x,y)rem(y,x)==0,[3,5,7,11,13]',0:9);
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!