Filter löschen
Filter löschen

How can I optimize this code for vector normalization?

2 Ansichten (letzte 30 Tage)
Valeria Iazzetta
Valeria Iazzetta am 30 Apr. 2023
Kommentiert: albara am 1 Mai 2023
Hi, I implemented this code in order to do the vector normalization of raman intensities. It works, but it is very slow.
%% Vector normalization
% PASSO 1: Calcolare i valori di intensità media per tutti i numeri d'onda
% Le intensità sono salvate in Spectra2
% I ramanshift della fingerprint sono 420
int_FP = load("int_FP.csv");
k = length(int_FP);
nn= size(int_FP);
n= nn(2);
am = zeros(1,n);
corr = zeros(k,1);
for i=1:n
for ii=1:k
corr(ii) = int_FP(ii,i);
end
am(i) = sum(corr);
am(i) = am(i)/k;
end
% PASSO 2: Sottraggo alle intensità l'intensità media
clear i ii;
a_k = zeros(k,n);
for ii=1:n
for i=1:k
a_k(i,ii) = int_FP(i,ii) - am(ii)
end
end
% PASSO 3:
% Implementare la formula aa_k = a_k/(rad(sum(a_k)^2))
clear i ii corr;
corr = zeros(k,1);
den = zeros(1,n);
for i=1:n
for ii=1:k
corr(ii) = a_k(ii,i);
end
den(i) = sqrt(sum((corr).^2));
end
clear i ii;
aa_k = zeros(k,n);
for ii=1:n
for i=1:k
aa_k(i,ii) = (a_k(i,ii))./den(ii);
end
end
% Verifica algoritmo
clear i ii;
corr = zeros(k,1);
leng = zeros(1,n);
for i=1:n
for ii=1:k
corr(ii) = aa_k(ii,i);
end
leng(i) = sum((corr).^2);
% leng è 1 per ogni spettro!
end
int_FP = aa_k;
How can I optimize this code?
Thanks to anyone who wants to help me.

Akzeptierte Antwort

albara
albara am 30 Apr. 2023
I can see that you're using nested for loops to perform various calculations, which can slow down your code significantly. In MATLAB, it's often faster to use vectorized operations instead of loops. Here's an optimized version of your code:
%% Vector normalization
% PASSO 1: Calcolare i valori di intensità media per tutti i numeri d'onda
% Le intensità sono salvate in Spectra2
% I ramanshift della fingerprint sono 420
int_FP = load("int_FP.csv");
[k, n] = size(int_FP);
% Calculate the mean intensity
am = sum(int_FP, 1) ./ k;
% PASSO 2: Sottraggo alle intensità l'intensità media
a_k = int_FP - repmat(am, k, 1);
% PASSO 3:
% Implementare la formula aa_k = a_k/(rad(sum(a_k)^2))
den = sqrt(sum(a_k.^2, 1));
aa_k = a_k ./ repmat(den, k, 1);
% Verifica algoritmo
leng = sum(aa_k.^2, 1);
int_FP = aa_k;
This optimized code should be much faster. I've replaced the nested loops with vectorized operations, making use of functions like sum, repmat, and element-wise operations. The optimized code should give you the same results as the original code but with improved performance.
Important: There may be some mistakes in this answer Experts can tell if there are any mistakes and you might find better answer
Kindly fgive your feedback
  2 Kommentare
Valeria Iazzetta
Valeria Iazzetta am 1 Mai 2023
It works great this way and I will make use of this procedure in the future. Thanks so much for helping me.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by