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!