How can I optimize this code for vector normalization?

조회 수: 2 (최근 30일)
Valeria Iazzetta
Valeria Iazzetta 2023년 4월 30일
댓글: albara 2023년 5월 1일
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.

채택된 답변

albara
albara 2023년 4월 30일
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
Valeria Iazzetta
Valeria Iazzetta 2023년 5월 1일
It works great this way and I will make use of this procedure in the future. Thanks so much for helping me.
albara
albara 2023년 5월 1일
You are welcome

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by