How Can i speed up "for" loop ?
Show older comments
Hello to everyone, I need to speed up my for loop. I use this loop for filling a vector with a temperature profile (that changes over time). I don't know if this is the fastest way so I am asking for your help. Thanks in advance.
T=25 + 273;
Tmax= 1100+273;
h=0.001;
temporaggiungimento=50;
tmax=100;
passo=( Tmax / temporaggiungimento)*h;
profilo1=linspace(25+273,1100+273,temporaggiungimento/h);
tempo=zeros(1,tmax/h);
profilo=zeros(1,tmax/h);
profilo(1)=25+273;
for j=1:(temporaggiungimento/h-1)
j=j+1
%profilo(j)=profilo(j-1)+passo;
profilo(j)=profilo1(j);
for j=temporaggiungimento/h:(tmax/h)-1
j=j+1;
profilo(j)=1100+273;
end;
end;
Accepted Answer
More Answers (1)
Mina Mino
on 7 Apr 2023
0 votes
I have the same problem and I need to speed up my code in the following form. I would be grateful if anyone can help me. it is so urgent and important for me. Thanks.
%begining of the Code%
clc
clear all
close all
format long g
fid=fopen('list2.txt','r');
if fid==-1
disp('there is an error')
else
end
S = textscan(fid,'%s');
fclose(fid);
i=1;
fnames= S{i,i};
tic
NA=[];
for fiile=1:700
varargout = readhgt(fnames{fiile});
LAT=varargout.lat;LAT(end)=[];
LON=varargout.lon;LON(end)=[];
Z=varargout.z;Z(:,end)=[];
Z(end,:)=[];
% Z_vec=reshape(Z,[12967201,1]);
ROUGH=[];
for row=1:3:3597
for col=1:3:3597
A=[Z(row,col) Z(row,col+1) Z(row,col+2);Z(row+1,col) Z(row+1,col+1) Z(row+1,col+2);Z(row+2,col) Z(row+2,col+1) Z(row+2,col+2)];
Dif=(double(A)-mean(mean(A))).^2;
rough=sqrt(mean(mean(Dif)));
ROUGH=[ROUGH;mean([LAT(row) LAT(row+1) LAT(row+2)]) mean([LON(col) LON(col+1) LON(col+2)]) rough];
end
end
NA=[NA;ROUGH];
end
toc
%end of code
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!