storing a code in a for loop

1 回表示 (過去 30 日間)
tina
tina 2022 年 8 月 30 日
編集済み: Subhajyoti 2024 年 12 月 8 日
hi, could anyone help me here? i've been given a task to store the code attached into one big for loop which executes the code and runs it all in one go then gives the image from the frames in the code. How can i do this?
clc
clear all
f=1;
N = 330;
M = nan(156,206,N);
for ii = 1:N
M(:,:,ii) = load(sprintf('frames_%d.txt',ii-1));
end
b=load('timestamp.txt');
for i=2:N
b(1)=0;
b(i)=b(i)+b(i-1);
end
c=b(1:N);
% b=b';
% MM=M(:,:,1:length(t));
% t=(0:2970)/33;
% MM=M(:,:,1:length(t));
% f=2;
% for i=1:156
% for j=1:207
% I(i,j,:)=squeeze(MM(i,j,:))'.*sin(2*pi*f.*t);
% Q(i,j,:)=squeeze(MM(i,j,:))'.*cos(2*pi*f.*t);
% end
% end
for i=1:N
I(:,:,i)=squeeze(M(:,:,i))'.*sin(2*pi*f.*c(i));
Q(:,:,i)=squeeze(M(:,:,i))'.*cos(2*pi*f.*c(i));
end
II=sum(I,3);
QQ=sum(Q,3);
Amp=sqrt(II.^2+QQ.^2);dlk
figure; imagesc(Amp)

回答 (1 件)

Subhajyoti
Subhajyoti 2024 年 12 月 8 日
編集済み: Subhajyoti 2024 年 12 月 8 日
Hi @tina,
It is my understanding that you are trying to combine all the looped-operations into a single loop.
You can combine the code into a single loop, by integrating the operations that depend on the loop index. Here, in the following implementation, I have implemented the same while optimizing redundant operations.
f=1;
N = 330;
M = nan(156,206,N);
b=load('timestamp.txt');
b(1)=0;
c=0;
for ii = 1:N
M = load(sprintf('frames_%d.txt',ii-1));
if(ii>1)
c = c + b(ii);
end
I(:,:,ii)=squeeze(M)'.*sin(2*pi*f.*c);
Q(:,:,ii)=squeeze(M)'.*cos(2*pi*f.*c);
end
II=sum(I,3);
QQ=sum(Q,3);
Amp=sqrt(II.^2+QQ.^2);dlk
figure; imagesc(Amp)
Additionally, you can also refer to the following resource to know more about loop-controls in MATLAB:

カテゴリ

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