for loop sequence from the matrix

3 ビュー (過去 30 日間)
Turbulence Analysis
Turbulence Analysis 2021 年 1 月 22 日
回答済み: Sai Veeramachaneni 2021 年 1 月 27 日
Hi,
I intend to execute the for loop, in which sequence for given index needs to be called from a matrix. For e.g. I have a matrix named 'A', it got the discontinous numbers, like 10 to 19, then 120 to 150 and 238 to 247 so on.. now I ned to exeute the for loop with +5 and - 5 from the point where discontinuity exists, e.g. as shown in below code
I have attached the matrix A here. Please help with this...
for i = [5:1:15 115:1:125 233:1:243]
% my code
end
  8 件のコメント
Masoud Dorvash
Masoud Dorvash 2021 年 1 月 22 日
I'm not sure if this example can help you or not.
firstSeq = 5:10;
secondSeq = 50:55;
thirdSeq = 500:505;
seq = [firstSeq secondSeq thirdSeq];
B = seq;
for i = 1:length(firstSeq)
B(i) = seq(i) - 5;
end
for j = length(firstSeq)+1:length(firstSeq)+length(secondSeq)
B(j) =seq(j) - 10;
end
for k = length(firstSeq)+length(secondSeq)+1:length(firstSeq)+length(secondSeq)+length(thirdSeq)
B(k) =seq(k) - 100;
end
but what you asked is somehow needs an if in the loop because the first sequence of the Amatrix can get 10 numbers but as you said you want the second one to get 9 numbers so you need to add a simple if in the for loop.
This is what you get in the result,
seq = 5 6 7 8 9 10 50 51 52 53 54 55 500 501 502 503 504 505
B = 0 1 2 3 4 5 40 41 42 43 44 45 400 401 402 403 404 405
Hope this works for you.
Turbulence Analysis
Turbulence Analysis 2021 年 1 月 22 日
Sorry, this is different.
I got 10000 files inside the folder, I need to read only selected files as per the numbers prsent in the matirx A. If you open matrix, A it starts from 10 to 19, then 120 to 150 and then 238 to 247 with the last sequence being 9914 to 9953. Here, i have to read the files based discontinuity, for e.g. it starts with 10, so first seqeunce for loop should be 5:15, after 19 its starts with 120 to 150 and agian discontinous. So my second sequence would be 115: 125. Basically I need to read five files before and after the discontinous number...

サインインしてコメントする。

回答 (1 件)

Sai Veeramachaneni
Sai Veeramachaneni 2021 年 1 月 27 日
Your question can be split into two parts.
  • Identifying discontinous numbers in the matrix A.
  • Exeute the for loop with +5 and - 5 from each discontinuous number.
Step1:
discontinous = [A(1)]%Stores the vector of discontinous numbers
for i = 2:numel(A)
discontinous = [discontinous A(i)];
end
Step2:
for i = 1:numel(discontinous)
for j = discontinous(i)-5:discontinous(i)+5
%Required tasks here.
end
end

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by