I want to decrease the time my Script
Ältere Kommentare anzeigen
Elapsed time is 8.597842 seconds.
clear, clc
tic
load('Erros_a.mat'); load('Sp1_b.mat');
Relatorio = zeros(5000,4,'uint32');
Relatorio_final = zeros(10000,4,'uint32');
a = 10;
for i1 = 1:1
% BLOCO 1 — usa cache se existir, senão gera e salva
cache_file = sprintf('Erros_b_cache_%d.mat', i1);
if exist(cache_file, 'file')
load(cache_file);
else
linha_a = Erros_a(a,:);
vals = linha_a(linha_a > 0);
vals = vals(1:end-2);
nRef = numel(vals);
cols_b1 = 2*(double(vals)-1) + 1;
cols_b2 = 2*(double(vals)-1) + 2;
passa = (Sp1_b(:, cols_b1) == 0) & (Sp1_b(:, cols_b2) == 0);
col_dest_matrix = uint32(cumsum(passa, 2) - passa + 1);
Erros_b = zeros(3268760, 37, 'uint32');
for d = 1:nRef
linhas_ok = find(passa(:, d));
if ~isempty(linhas_ok)
idx = sub2ind([3268760, 37], linhas_ok, double(col_dest_matrix(linhas_ok, d)));
Erros_b(idx) = vals(d);
end
end
save(cache_file, 'Erros_b', '-v7.3');
end
% BLOCO 2 — preparação
load('Sp1_c.mat');
Erros_b(:,38) = (1:size(Erros_b,1))';
mascara = Erros_b(:, 13) > 50;
Erros_reduzido = Erros_b(mascara, :);
[lin_u, col_u] = find(Sp1_c == 1);
uns_nas_colunas = accumarray(col_u, lin_u, [size(Sp1_c,2) 1], @(x){sort(x)}, {[]});
clear Erros_b Sp1_c lin_u col_u mascara;
[~, ordem] = sortrows(Erros_reduzido(:, 1:38));
Erros_reduzido_ordem = Erros_reduzido(ordem, :);
[~, ordem] = sortrows(Erros_reduzido_ordem(:, 1:38));
Erros_reduzido_ordem = Erros_reduzido_ordem(ordem, :);
clear Erros_reduzido ordem;
ERO = double(Erros_reduzido_ordem);
clear Erros_reduzido_ordem;
nCols_c = numel(uns_nas_colunas);
uns_sets = cell(nCols_c, 1);
for cc = 1:nCols_c
s = uns_nas_colunas{cc};
if ~isempty(s)
m = false(max(s), 1);
m(s) = true;
uns_sets{cc} = m;
else
uns_sets{cc} = false(0,1);
end
end
% BLOCO 3 — while principal
num_linhas = size(ERO, 1);
y = 1;
offset = 0;
while (offset + 1) <= num_linhas
i = offset + 1;
linha_at = ERO(i, 1:38);
a_valid = linha_at(linha_at > 0 & linha_at <= 100);
% k=2: dois ponteiros
A = uns_nas_colunas{a_valid(1)};
B = uns_nas_colunas{a_valid(2)};
buf = zeros(min(numel(A), numel(B)), 1);
ia = 1; ib = 1; ic = 0;
na = numel(A); nb = numel(B);
while ia <= na && ib <= nb
if A(ia) == B(ib)
ic = ic + 1; buf(ic) = A(ia);
ia = ia + 1; ib = ib + 1;
elseif A(ia) < B(ib); ia = ia + 1;
else; ib = ib + 1;
end
end
candidatas = buf(1:ic);
w=1; z=1;
n(w,1) = a_valid(w,1);
achou = false;
if isempty(candidatas)
melhor_erro = a_valid(2) - 1;
melhor_linha = linha_at(38);
n(w,2) = a_valid(w,2);
nP = length(n);
lo = offset + 1; hi = num_linhas; ultlin_abs = offset;
if all(ERO(lo, 1:nP) == n)
while lo <= hi
mid = floor((lo + hi) / 2);
if all(ERO(mid, 1:nP) == n)
ultlin_abs = mid; lo = mid + 1;
else; hi = mid - 1; end
end
end
if ultlin_abs > offset; offset = ultlin_abs;
else; offset = offset + 1; end
n = [];
Relatorio(y,1) = Erros_a(i1,40);
Relatorio(y,2) = A(1);
Relatorio(y,3) = melhor_linha;
Relatorio(y,4) = melhor_erro;
y = y + 1;
achou = true;
end
% k>=3: lookup lógico
for k = 3:length(a_valid)
if achou; break; end
n(w,k-1) = a_valid(w,k-1);
col_atual = a_valid(k);
s2 = uns_sets{col_atual};
if isempty(candidatas) || isempty(s2)
proximas = [];
else
mask = candidatas <= numel(s2);
cand_validas = candidatas(mask);
if isempty(cand_validas); proximas = [];
else; proximas = cand_validas(s2(cand_validas)); end
end
if isempty(proximas)
melhor_erro = col_atual - 1;
melhor_linha = linha_at(38);
n(w,k) = a_valid(w,k);
nP = length(n);
lo = offset + 1; hi = num_linhas; ultlin_abs = offset;
if all(ERO(lo, 1:nP) == n)
while lo <= hi
mid = floor((lo + hi) / 2);
if all(ERO(mid, 1:nP) == n)
ultlin_abs = mid; lo = mid + 1;
else; hi = mid - 1; end
end
end
if ultlin_abs > offset; offset = ultlin_abs;
else; offset = offset + 1; end
n = [];
Relatorio(y,1) = Erros_a(i1,40);
Relatorio(y,2) = candidatas(1);
Relatorio(y,3) = melhor_linha;
Relatorio(y,4) = melhor_erro;
y = y + 1;
achou = true;
break;
end
candidatas = proximas;
melhor_linha = candidatas(1);
end % for k
if ~achou; offset = offset + 1; end
end % while
clear ERO uns_sets;
Relatorio = sortrows(Relatorio, 4, 'descend');
Relatorio_final(i1,:) = Relatorio(1,:);
Relatorio(:,:) = 0;
a = a + 1;
end % i1
toc
1 Kommentar
Walter Roberson
am 29 Mär. 2026 um 19:47
Not much in the way of comments. No comments about the purpose of the code, or how it operates. Not worth examining.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Introduction to Installation and Licensing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!