%% ============================================================ % LEVERAGE SCORE DEMONSTRATION (MATLAB ONLY) % Shows WHY leverage uses U_k, not A. % Produces plots + heatmaps for direct comparison. %% ============================================================ close all; clear; clc; %% ---- Load 256x256 grayscale image ---- A = double(imread('cameraman.bmp')); A = A(:,:,1); % ensure grayscale [m,n] = size(A); %% ---- Choose rank k ---- k = 50; % good test value %% ---- Compute rank-k SVD ---- [U,S,V] = svds(A, k); %% ---- TRUE leverage scores (from U_k) ---- true_row_lev = sum(U.^2, 2); % m×1 true_col_lev = sum(V.^2, 2); % n×1 %% ---- FAKE leverage scores (wrong: directly from A) ---- fake_row_lev = sum(A.^2, 2); % sum of row squares fake_col_lev = sum(A.^2, 1)'; % sum of col squares %% ---- Normalize both for comparison ---- true_row_lev = true_row_lev / sum(true_row_lev); fake_row_lev = fake_row_lev / sum(fake_row_lev); true_col_lev = true_col_lev / sum(true_col_lev); fake_col_lev = fake_col_lev / sum(fake_col_lev); %% ============================================================ % VISUALIZATIONS % ============================================================ figure('Position',[200 200 1200 700]); %% ---- Plot row leverage scores ---- subplot(2,2,1); plot(true_row_lev, 'LineWidth', 2); hold on; plot(fake_row_lev, '--', 'LineWidth', 2); title('Row Leverage Scores'); legend('True leverage (U_k)', 'Fake leverage (A)', 'Location','Best'); xlabel('Row index'); ylabel('Score'); %% ---- Plot column leverage scores ---- subplot(2,2,2); plot(true_col_lev, 'LineWidth', 2); hold on; plot(fake_col_lev, '--', 'LineWidth', 2); title('Column Leverage Scores'); legend('True leverage (V_k)', 'Fake leverage (A)', 'Location','Best'); xlabel('Column index'); ylabel('Score'); %% ---- Heatmap of true leverage (rows) ---- subplot(2,2,3); imagesc(repmat(true_row_lev, 1, n)); colormap turbo; colorbar; title('TRUE Row Leverage Heatmap (from U_k)'); xlabel('Column'); ylabel('Row'); %% ---- Heatmap of fake leverage (rows) ---- subplot(2,2,4); imagesc(repmat(fake_row_lev, 1, n)); colormap turbo; colorbar; title('FAKE Row Leverage Heatmap (from A)'); xlabel('Column'); ylabel('Row'); sgtitle('Why CUR Uses U_k for Leverage Scores, Not A', 'FontSize', 16);