필터 지우기
필터 지우기

for loop help

조회 수: 1 (최근 30일)
James
James 2011년 8월 7일
Hi
I want to be able to select c = 2 3 5 6 8 9 12 13 15 16 18 19 22 23 25 26 28 29 32....
I need to do this all the way up to 89. Can i do this in a loop?

답변 (8개)

Neels
Neels 2011년 8월 7일
hi James, can you tell if the series follows a specific sequence or just some random values

Tim Mottram
Tim Mottram 2011년 8월 7일
Hi James,
So you want all numbers accept those ending in a 0,1 or 4? If this is correct then put an IF statement inside your FOR loop with something like: if a~= 0 && a~= 1 && a~=4, where a is the last digit of the current step being used by the for loop. Put the for loop incrementer (i = i+1) after the END of this IF statement. How you calculate, a, is up to you...
Regards
Tim
  댓글 수: 2
James
James 2011년 8월 7일
i suppose when you look at it that way then yes i need to skip all numbers ending in 0,1,4 and 7
how do i select the last digit of the current step?
Tim Mottram
Tim Mottram 2011년 8월 7일
see Jan's comment for selecting the last digit, or transform the number into a string,(string = num2str(current for loop)) then ask for its length, (digit = length(string)) and if digit (this will always be the last digit) of string is not equal to 0,1,4 or 7, then proceed. some rough code..
for i = 0:YourEndPoint
s = num2str(i);
d = length(s);
if s(d) ~= 0 && ~=1 && ~= 4 && ~= 7
your code for what you want
end
i = i+1
end
Sorry about the late reply, people are really on it here and you probably already have your solution.

댓글을 달려면 로그인하십시오.


Jan
Jan 2011년 8월 7일
for i = 0:90
if all(mod(i, 10) ~= [0,1,4,7])
disp(i)
end
end
  댓글 수: 2
James
James 2011년 8월 7일
how does the if line work?
all(mod(i, 10) ~= [0,1,4,7])
Jan
Jan 2011년 8월 7일
@James: Read it loud: if all (reminder of i divided by 10) are not equal to an element of [0, 1, 4, 7].
Example: i = 18, mod(i, 10)=8, (8~=[0,1,4,7]) = [1,1,1,1], all([1,1,1,1]) = TRUE.
i = 17, mod(i, 10)=7, (7~=[0,1,4,7])=[1,1,1,0], all([1,1,1,0]) = FALSE

댓글을 달려면 로그인하십시오.


Jan
Jan 2011년 8월 7일
Another method:
for a = 0:10:80
for b = [2,3,5,6,8,9]
k = a + b;
disp(k);
end
end

Jan
Jan 2011년 8월 7일
And if I'm on the way:
ind = bsxfun(@plus, 0:10:80, [2,3,5,6,8,9]');
for i = reshape(ind, 1, [])
disp(i)
end

James
James 2011년 8월 7일
Thanks. I was also just wondering if you can help me with something else similar:
for c = 11:20
if c~=11 && c~=14 && c~=17 && c~=20
break
end
% how can I loop through for all values of r, which are r=11,14,17,20????
end
  댓글 수: 2
Jan
Jan 2011년 8월 7일
@James: ??? Do you mean: for r = [11, 14, 17, 20], ... end
Tim Mottram
Tim Mottram 2011년 8월 7일
for i = 11:3:20 ??

댓글을 달려면 로그인하십시오.


Paulo Silva
Paulo Silva 2011년 8월 7일
yet another way
r=9; %number of repetitions of the sequence, ex 9, max is 89
b=[2 3 5 6 8 9]; %original sequence
c=repmat(b,r,1); %repeat the sequence in each row
d=0:10:10*r-1; %create vector with sum values
e=repmat(d,6,1)'; %create array from vector
f=(c+e)'; %now all in the right place do the sum
f(:)' %put the values in just a vector

Andrei Bobrov
Andrei Bobrov 2011년 8월 7일
a=1:90;
k = reshape(a,10,[]);
k(1:3:end,:)=[];
c = k(:)'
ADD
a = reshape(1:90,10,[])';
c = [];
for j1 = 1:size(a,1)
for j2 = 1:3
c = [c a(j1,j2*3-[1 0])];
end
end
MORE
a = reshape(1:90,10,[]);
c=reshape(a(bsxfun(@minus,(3:3:9),[1 0]'),:),1,[])

카테고리

Help CenterFile 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