How to speed up operation in two 'for' loops?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Only k cycles take 20 seconds,it will waste more time with i cycle.How can I improve?
0 Commenti
Risposta accettata
Stephen23
il 14 Dic 2022
Modificato: Stephen23
il 14 Dic 2022
"How can I improve?"
In general importing/exporting data is slow. So rather than calling READMATRIX 58732*numel(S) times in a loop, just call READMATRIX numel(S) in a loop and manipulate the imported matrices in MATLAB memory. Your code will also be slow because NUM is not preallocated:
The second (58732) loop looks superfluous (you just seem to be allocating scalars to NUM), so most likely you can you efficiently concatenate the imported vectors/matrices after the loop.
P = 'D:\elevation';
S = dir(fullfile(P,'*.dat'));
for k = 1:numel(S)
F = fullfile(S(K).folder,S(k).name);
M = readmatrix(F);
S(k).data = M(:,6);
end
A = [S.data].'
Note that depending on the filenames, the order of files returned by DIR() might not what you expect (and so your concatenated data might not be in the order you expect).
4 Commenti
Stephen23
il 15 Dic 2022
Modificato: Stephen23
il 15 Dic 2022
"How to convert longitude and latitude coordinate axis to xy coordinate"
Is this what you are looking for?:
N = 301;
X = linspace(110,130,N)
Y = linspace(35,42.2,N)
Note that 1:300 is only 299 equal parts, see https://en.wikipedia.org/wiki/Off-by-one_error#Fencepost_error
Or perhaps you want to convert lat/long data into X/Y data, e.g.:
V = 35+(42.2-35)*rand(1,7) % random latitude data with range from 35 to 42.2
X = interp1([35,42.2],[1,N],V) % converted to range from 1 to 301
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!