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!