I'm a new guy for matlab loop. Righi now I want to now some rools about loop(for...end). Such as how to make a loop to get all odd numbers from a matrix x=[1:100] or numbers like 1,5,9,13,17,21...? p.s.I know this x1=x(1:2:100) and x4=(1:4:100),but I want to know how to get it from a loop(for...end).

 Réponse acceptée

Matt Fig
Matt Fig le 22 Mar 2011

0 votes

It would be good if you learned to pre-allocate your vectors so your code runs efficiently...
.
.
.
EDIT In response to question about generalization.
The general case can be written:
N = 100; % The largest number. Change to whatever...
a = 1; % The starting point. Change to 3,5... whatever
n = zeros(1,ceil((N-a)/2)); % Pre-allocate the array...
for ii = 1:length(n)
n(ii) = 2*(ii)+(a-2);
end

9 commentaires

Paulo Silva
Paulo Silva le 22 Mar 2011
Matt that will introduce some problems that the OP might not understand yet
Matt Fig
Matt Fig le 22 Mar 2011
Hmm, the only problem I see is how to calculate an odd number from any integer k, 2*k-1. That seems trivial, what problems are you talking about? It seems to me that folks might as well learn to do things the correct way, or they will come back later and say, "Why is my code sooo slow?"
Paulo Silva
Paulo Silva le 22 Mar 2011
If you don't know the space needed to allocate? you allocate more than enough and have to discard part of the values, not a big problem but might confuse new users.
Tian Lin
Tian Lin le 22 Mar 2011
hi,Matt,If the matrix likes x=[1.6,2.7,3.5,4.4,5.2,6.8,7.9,8.4,9.1,10.2],how can I use your code to get this a=[1.6,3.5,5.2,7.9,9.1]?
Matt Fig
Matt Fig le 22 Mar 2011
That is a different question!
N = length(x); % The largest number. Change to whatever...
a = 1; % The starting point. Change to 3,5... whatever
n = zeros(1,ceil((N-a)/2)); % Pre-allocate the array...
for ii = 1:length(n)
n(ii) = x(2*(ii)+(a-2));
end
Matt Fig
Matt Fig le 22 Mar 2011
Note the change to N in the previous response....
Tian Lin
Tian Lin le 22 Mar 2011
thanks,I try to get [1.6,4.4,7.9,10.2] and change the code like this:
N = length(x); % The largest number. Change to whatever...
a = 1; % The starting point. Change to 3,5... whatever
n = zeros(1,ceil((N-a)/3)); % Pre-allocate the array...
for ii = 1:length(n)
n(ii) = x(3*(ii)+(a-3));
end
but only got [1.6,4.4,7.9],there was no 10.2 in it,what's wrong with my changing?
Matt Fig
Matt Fig le 22 Mar 2011
Boy, you keep changing the problem! That is o.k., you will just have to realize that changing the problem changes the approach:
N = length(x); % The largest number. Change to whatever...
a = 1; % The starting point. Change to 3,5... whatever
S = 3;
n = zeros(1,floor((N-a)/(S))+1); % Pre-allocate the array...
for ii = 1:length(n)
n(ii) = x(S*(ii)+(a-S));
end
Tian Lin
Tian Lin le 22 Mar 2011
man,that's cool,I only need to change S and a to get the best result.Thank you very much.I believe some day I can write the code by myself,like you.

Connectez-vous pour commenter.

Plus de réponses (3)

Paulo Silva
Paulo Silva le 22 Mar 2011

0 votes

n=[];
for a=1:2:100
n=[n a];
end

3 commentaires

Tian Lin
Tian Lin le 22 Mar 2011
thanks,if I want to do this: a=2:2:100,a=3:2:100,a=4:2:100,...,a=100:2:100.how to write a loop?
Walter Roberson
Walter Roberson le 22 Mar 2011
for K = 1:100
a = K:2:100;
%here, do something with the vector "a"
end
Tian Lin
Tian Lin le 22 Mar 2011
if I have numbers larger than 100,just like 10000,whether I will write 10000 "for"?

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 22 Mar 2011

0 votes

Please don't do that: please read this FAQ instead.
Paulo Silva
Paulo Silva le 22 Mar 2011

0 votes

clc
n={};
c=0;
for b=2:100
n1=[];
for a=b:2:100
n1=[n1 a];
end
c=c+1;
n{c,1}=n1;
end
The result is inside the n variable, n is a cell, each element of it contains the results n{1,1} gives you the odd numbers for 2:2:100, n{2,1} gives the odd numbers for 3:2:100 and so on...

1 commentaire

Tian Lin
Tian Lin le 22 Mar 2011
thank you Paulo.It's hard to me to understand something like reduce using memory.Also,I don't know the cell very much,so I find Matt's code much esaier.whatever,thanks a lot.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by