for loop sequence from the matrix

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 Kommentare

Mathieu NOE
Mathieu NOE am 22 Jan. 2021
hello
ok, but what are we suppose to do in the code , especially for i that is not present in the A vector ?
Turbulence Analysis
Turbulence Analysis am 22 Jan. 2021
I need to read the files present in the folder. It got 10000 files from 1 to 10000. But I need to read only set of files as per the conditions I mentioned ....
Masoud Dorvash
Masoud Dorvash am 22 Jan. 2021
Bearbeitet: Masoud Dorvash am 22 Jan. 2021
Hi there,
I really can't unerstand your question, you want that the for loop generates a new matrix which elements are from the A matrix with the position i ?
Turbulence Analysis
Turbulence Analysis am 22 Jan. 2021
seq = {5:15; 115:125; 233:243 ...... so on};
for j = 1:numel(seq)
for kk = seq{j}
if (kk>=1) && (kk<=9)
fname_strt = 'B0000' ;
elseif (kk>=10) && (kk<=99)
fname_strt='B000';
elseif (kk>=100) && (kk<=999)
fname_strt='B00';
elseif (kk>=1000) && (kk<=9999)
fname_strt='B0';
else
fname_strt='B';
end
fname_end = num2str(kk);
fname = strcat(fname_strt,fname_end,'.vc7');
II = loadvec(fname);
x=(II.x);
y=(II.y);
curl = vec2scal(II,'rot');
fu2=flipud(curl.w');
imshow(fu2);
end
end
Hi, shown above, is my full code.
As you can see, I am executing for loop in seqeunce. The numbers in the seq needs to be identified from matrix A based on the condition which I detailed below..
For e.g., In the matrix A, it starts with 10 to 19, then 120 to 150 and so on. Here my first sequence should be 5 to 15 , next will be 115 to 125 etc.,
Stephen23
Stephen23 am 22 Jan. 2021
Bearbeitet: Stephen23 am 22 Jan. 2021
As an aside, you should replace all of that complex IF/ELSEIF/ELSE, NUM2STR, and STRCAT with this:
fname = sprintf('B%05d.vc7',j)
Turbulence Analysis
Turbulence Analysis am 22 Jan. 2021
Hi,
Yes that coulde be done. But I intend to create the sequence from the Matrix A.. (attached here)..
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 am 22 Jan. 2021
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...

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Sai Veeramachaneni
Sai Veeramachaneni am 27 Jan. 2021

0 Stimmen

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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by