Can anyone help me optimize the if statements in For loops to make the code faster?
15 views (last 30 days)
Show older comments
function [triu_adjacency,J_indices] = get_triu_adjacency(J_org, max_num_neighbors)
% J_org =[ 0 -1 -1 1 2
% -1 0 -1 1 2
% -1 -1 0 1 2
% 1 1 1 0 -2
% 2 2 2 -2 0];
%
% J_org = [ 0, +2, +0, +1, +0, -1, -1
% +2, 0, +0, -1, -1, +3, +0
% +0, +0, 0, -2, +0, +1, +1
% +1, -1, -2, 0, +2, +0, +0
% +0, -1, +0, +2, 0, +0, +1
% -1, +3, +1, +0, +0, 0, +0
% -1, +0, +1, +0, +1, +0, 0];
J_org = full(J_org);
J = triu(J_org);
num_bits = length(J_org);
%max_neighbors = max(degree(graph(J_org)));
% max_neighbors =7
counter = 1;
zero_counter =[];
triu_adjacency = zeros(num_bits, max_num_neighbors);
J_indices = zeros(size(J));
for i= 1:num_bits
n_neighbors = 1;
col_list = J(1:i,i);
for j = 1:length(col_list)
if col_list(j)
triu_adjacency(i,n_neighbors) = J_indices(j,i);
n_neighbors = n_neighbors + 1;
end
end
row_list = J(i,i+1:end);
for j = 1:length(row_list)
if row_list(j)
triu_adjacency(i,n_neighbors) = counter;
J_indices(i, j + i) = counter;
counter = counter +1;
n_neighbors = n_neighbors + 1;
end
end
for k= n_neighbors:max_num_neighbors
if isempty(zero_counter)
triu_adjacency(i, k) = counter;
zero_counter = counter;
counter = counter +1;
else
triu_adjacency(i, k) = zero_counter;
zero_counter = [];
end
end
end
end
2 Comments
Accepted Answer
Jan
on 9 May 2022
The loops look fine. I'd omit the J=triu(J_org), because you use the upper triangle only inside the code
Vectorizing the inner loops increases the processing time by a factor of 2! See:
function [Adj,Index] = get_triu_adjacency(JS, nNeighbors)
% This is SLOWER then the original nested loops!!!
J = full(JS) ~= 0;
nBits = length(J);
Adj = zeros(nBits, nNeighbors);
Index = zeros(size(J));
c = 1;
z = nan;
for i = 1:nBits
col = J(1:i, i);
nc = sum(col);
Adj(i, 1:nc) = Index(col, i);
row = J(i, i+1:nBits);
nr = sum(row);
Adj(i, nc+1:nc+nr) = c:c+nr-1;
Index(i, find(row) + i) = c:c+nr-1;
c = c + nr - 1;
n = nc + nr - 1;
for k = n:nNeighbors
if isnan(z)
Adj(i, k) = c;
z = c;
c = c + 1;
else
Adj(i, k) = z;
z = nan;
end
end
end
end
I've simplified the names of the variables to reduce the clutter. Compare "n_neighbors" with a simple "n".
Setting z to NaN instead of the empty matrix might save some milliseconds. But the loop version seems to be efficient.
3 Comments
Jan
on 14 May 2022
Measuring the timings reduces the pörocessing speed in general, not only for this code. To determine the need spent in each line, Matlab must record the timings after each command. Of course this is expensive.
In addition Matlab's profiler disables the JIT acceleration (at least partially): The JIT can reorder commands inside loops to improve the speed. But then Matlab cannot determine the time spent in a specific line anymore. This means, that the profiler has a strong influence on the processing speed - what a pity.
More Answers (0)
See Also
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!