# save images inside a for loop at uneven intervals

1 Ansicht (letzte 30 Tage)
Turbulence Analysis am 25 Jan. 2024
Bearbeitet: Matt J am 25 Jan. 2024
Hi,
I am trying to save images inside the Img array that is generated inside the for loop at uneven iteration numbers. For example sometimes image generated at 10th iteration, while sometimes it's at 150th iteration. How to handle this?
for i = 1:a
B = some process % my image
Img(:,:,) = B
end
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Voss am 25 Jan. 2024
Img = zeros(0,0,0);
for i = 1:a
B = some process % my image
if ... % if some condition says to store this B in Img
Img(:,:,end+1) = B;
end
end
##### 6 Kommentare4 ältere Kommentare anzeigen4 ältere Kommentare ausblenden
Voss am 25 Jan. 2024
Maybe something along these lines:
Img = zeros(0,0,0);
last_stored_time = ts(1);
for i = 1:a
B = some process % my image
if ts(i) - last_stored_time >= 200
Img(:,:,end+1) = B;
last_stored_time = ts(i);
end
end
Turbulence Analysis am 25 Jan. 2024
Thanks very much!

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (2)

Matt J am 25 Jan. 2024
Bearbeitet: Matt J am 25 Jan. 2024
One way,
Img=nan(M,N,a);
Isubset=[10 47, 150,...,a]
for i = 1:a
B = some process % my image
if ismember(i,Isubset)
Img(:,:,i) = B;
end
end
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Turbulence Analysis am 25 Jan. 2024
Verschoben: Voss am 25 Jan. 2024
Hi Matt,
Actually iteration number 10, 150 is just an example, in reality these numbers are highly random.

Melden Sie sich an, um zu kommentieren.

Matt J am 25 Jan. 2024
Bearbeitet: Matt J am 25 Jan. 2024
If you don't know in advance which and how many loop iterations you'll be storing, it would be best to accumulate them in a cell array, and then post-concatenate:
Img = cell(1,a);
for i = 1:a
B = some process % my image
if ... % if some condition says to store this B in Img
Img{i} = B;
end
end
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Image Arithmetic finden Sie in Help 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