Seeking Help to Optimize MATLAB Function for reducing running time
Dear MATLAB Community,
I hope this message finds you well. I’m currently working on a project that involves optimizing a MATLAB function for improved performance, and I’m reaching out to seek your valuable assistance.
The function is a crucial component of my project. While it serves its purpose, I believe there is room for enhancement in terms of its execution speed. Now it takes more than 1 hours but I want if we can do it in 20-25 min.
I’ve explored various optimization techniques, including vectorization and sparse matrix usage, but I’m still encountering performance bottlenecks.
If you have experience or expertise in optimizing MATLAB functions or tackling similar challenges, I would greatly appreciate any insights, suggestions, or optimizations you could offer.
Your assistance would not only benefit my project but also contribute to the collective knowledge and expertise of the MATLAB community.
Thank you for considering my request, and I look forward to any guidance or assistance you can provid. der1 and der2 are large file more than 5 mb. So I am unable to attach them. each is a cell of {10980*1} and each elemet of 10980*10980. thanks
function J = customJ(der1, der2, smat, DTM)
% %Calculate system matrix
% der1=der1;
% der2=der2;
% smat =smat;
% phi =phi;
% z = z;
load(‘der1.mat’)
load(‘der2.mat’)
load(‘smat.mat’)
load(‘phi.mat’)
load(‘z.mat’)
Ismart =inv(smat);
n = 10980;
% Preallocate memory for derivative matrices
ns = 28;
nd = 28;
%Jmua = zeros(ns * nd, n);
%Jmus = zeros(ns * nd, n);
Jmua = spalloc(ns * nd, n, 10*ns*nd); % Preallocate sparse matrix
Jmus = spalloc(ns * nd, n, 10*ns*nd); % Preallocate sparse matrix
% Calculate full matrices directly
for ii = 1:n
% Display progress
disp([‘column wise: ‘ num2str(round(ii/n*100)) ‘%’])
dm0dmua = -Ismart * (der1{ii} * phi);
dm0dmus = -Ismart * (der2{ii} * phi);
% Calculate Jmua and Jmus directly
temp1 = DTM.’ * dm0dmua;
temp2 = DTM.’ * dm0dmus;
temp3 = z;
Jmua(:,ii) = temp1(:)./temp3(:);
Jmus(:,ii) = temp2(:)./temp3(:);
end
% Concatenate Jmua and Jmus
J = [real([Jmua, Jmus]); imag([Jmua, Jmus])];
endDear MATLAB Community,
I hope this message finds you well. I’m currently working on a project that involves optimizing a MATLAB function for improved performance, and I’m reaching out to seek your valuable assistance.
The function is a crucial component of my project. While it serves its purpose, I believe there is room for enhancement in terms of its execution speed. Now it takes more than 1 hours but I want if we can do it in 20-25 min.
I’ve explored various optimization techniques, including vectorization and sparse matrix usage, but I’m still encountering performance bottlenecks.
If you have experience or expertise in optimizing MATLAB functions or tackling similar challenges, I would greatly appreciate any insights, suggestions, or optimizations you could offer.
Your assistance would not only benefit my project but also contribute to the collective knowledge and expertise of the MATLAB community.
Thank you for considering my request, and I look forward to any guidance or assistance you can provid. der1 and der2 are large file more than 5 mb. So I am unable to attach them. each is a cell of {10980*1} and each elemet of 10980*10980. thanks
function J = customJ(der1, der2, smat, DTM)
% %Calculate system matrix
% der1=der1;
% der2=der2;
% smat =smat;
% phi =phi;
% z = z;
load(‘der1.mat’)
load(‘der2.mat’)
load(‘smat.mat’)
load(‘phi.mat’)
load(‘z.mat’)
Ismart =inv(smat);
n = 10980;
% Preallocate memory for derivative matrices
ns = 28;
nd = 28;
%Jmua = zeros(ns * nd, n);
%Jmus = zeros(ns * nd, n);
Jmua = spalloc(ns * nd, n, 10*ns*nd); % Preallocate sparse matrix
Jmus = spalloc(ns * nd, n, 10*ns*nd); % Preallocate sparse matrix
% Calculate full matrices directly
for ii = 1:n
% Display progress
disp([‘column wise: ‘ num2str(round(ii/n*100)) ‘%’])
dm0dmua = -Ismart * (der1{ii} * phi);
dm0dmus = -Ismart * (der2{ii} * phi);
% Calculate Jmua and Jmus directly
temp1 = DTM.’ * dm0dmua;
temp2 = DTM.’ * dm0dmus;
temp3 = z;
Jmua(:,ii) = temp1(:)./temp3(:);
Jmus(:,ii) = temp2(:)./temp3(:);
end
% Concatenate Jmua and Jmus
J = [real([Jmua, Jmus]); imag([Jmua, Jmus])];
end Dear MATLAB Community,
I hope this message finds you well. I’m currently working on a project that involves optimizing a MATLAB function for improved performance, and I’m reaching out to seek your valuable assistance.
The function is a crucial component of my project. While it serves its purpose, I believe there is room for enhancement in terms of its execution speed. Now it takes more than 1 hours but I want if we can do it in 20-25 min.
I’ve explored various optimization techniques, including vectorization and sparse matrix usage, but I’m still encountering performance bottlenecks.
If you have experience or expertise in optimizing MATLAB functions or tackling similar challenges, I would greatly appreciate any insights, suggestions, or optimizations you could offer.
Your assistance would not only benefit my project but also contribute to the collective knowledge and expertise of the MATLAB community.
Thank you for considering my request, and I look forward to any guidance or assistance you can provid. der1 and der2 are large file more than 5 mb. So I am unable to attach them. each is a cell of {10980*1} and each elemet of 10980*10980. thanks
function J = customJ(der1, der2, smat, DTM)
% %Calculate system matrix
% der1=der1;
% der2=der2;
% smat =smat;
% phi =phi;
% z = z;
load(‘der1.mat’)
load(‘der2.mat’)
load(‘smat.mat’)
load(‘phi.mat’)
load(‘z.mat’)
Ismart =inv(smat);
n = 10980;
% Preallocate memory for derivative matrices
ns = 28;
nd = 28;
%Jmua = zeros(ns * nd, n);
%Jmus = zeros(ns * nd, n);
Jmua = spalloc(ns * nd, n, 10*ns*nd); % Preallocate sparse matrix
Jmus = spalloc(ns * nd, n, 10*ns*nd); % Preallocate sparse matrix
% Calculate full matrices directly
for ii = 1:n
% Display progress
disp([‘column wise: ‘ num2str(round(ii/n*100)) ‘%’])
dm0dmua = -Ismart * (der1{ii} * phi);
dm0dmus = -Ismart * (der2{ii} * phi);
% Calculate Jmua and Jmus directly
temp1 = DTM.’ * dm0dmua;
temp2 = DTM.’ * dm0dmus;
temp3 = z;
Jmua(:,ii) = temp1(:)./temp3(:);
Jmus(:,ii) = temp2(:)./temp3(:);
end
% Concatenate Jmua and Jmus
J = [real([Jmua, Jmus]); imag([Jmua, Jmus])];
end matrix, multiplication, sparse MATLAB Answers — New Questions