how to make loop

조회 수: 4 (최근 30일)
Tian Lin
Tian Lin 2011년 3월 22일
I have asked the question as http://www.mathworks.com/matlabcentral/answers/3748-how-to-make-a-loop-for-end. Matt fig helped me this: x=[1.6,2.7,3.5,4.4,5.2,6.8,7.9,8.4,9.1,10.2]; 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 Now,I want to make a new loop for S (S=1:length(x)),that gets the value of S from 1 to length(x).So I can get every value of n when the S is different. How are the codes about?
  댓글 수: 1
Andrew Newell
Andrew Newell 2011년 3월 22일
@Tian, it would really help those of us who answer your questions if you formatted your code: one line per command, indent two spaces.

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

채택된 답변

Matt Fig
Matt Fig 2011년 3월 22일
L = length(x)
N = L; % The largest number. Change to whatever...
a = 3; % The starting point. Change to 3,5... whatever
T = cell(1,L); % Store everything in a cell array.
for jj = 1:L
S = jj;
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
T{jj} = n;
end
T{:}
You should go back and format your question. Is there a particular reason why you want to do this with loops?
  댓글 수: 1
Tian Lin
Tian Lin 2011년 3월 22일
In the beginning,I only wanted to do a loop to get some data from matrix.With your help,I find some questions about changing the conditions of loops,so I asked again and again.Sorry for you,there isn't any particular reason.Next time,I will think over and then ask a question.By the way,I will say thank you agian.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by