You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

带显式零的Matlab稀疏矩阵格式转换技术问询

Converting MATLAB Sparse Matrices with Explicit Zeros to Harwell-Boeing Format

I get it—working with sparse matrices that have explicit zeros (marked as binary entries) can be tricky because MATLAB's default sparse functions automatically drop zero values. Your approach of temporarily replacing those zeros with ones to preserve their positions is spot-on; here's a polished, complete version of your code with explanations to make it robust:

function matlab_to_HB(matName, fileName, title, key, type)
    % Load the MATLAB data file containing the sparse matrix and explicit zeros
    data = load(matName);
    SMat = data.Problem.A; % Original sparse matrix with non-zero values
    
    % Extract non-zero indices and values
    [i_nonzero, j_nonzero, v_nonzero] = find(SMat);
    
    % Handle explicit zeros if they exist in the dataset
    if isfield(data.Problem, 'Zeros')
        zerosMat = data.Problem.Zeros; % Matrix marking explicit zero positions (binary)
        [i_zero, j_zero] = find(zerosMat); % Get indices of explicit zeros
        
        % Optional: Skip any zero positions that overlap with non-zero entries
        overlap_mask = ~ismember([i_zero, j_zero], [i_nonzero, j_nonzero], 'rows');
        i_zero = i_zero(overlap_mask);
        j_zero = j_zero(overlap_mask);
        
        % Combine non-zero and explicit zero indices; use 1 as a placeholder for zeros
        i_combined = [i_nonzero; i_zero];
        j_combined = [j_nonzero; j_zero];
        v_combined = [v_nonzero; ones(length(i_zero), 1)];
        
        % Create a sparse matrix that includes both non-zeros and explicit zero positions
        full_sparse = sparse(i_combined, j_combined, v_combined, size(SMat, 1), size(SMat, 2));
        
        % Reset the explicit zero positions back to 0
        full_sparse(sub2ind(size(full_sparse), i_zero, j_zero)) = 0;
    else
        % If no explicit zeros, use the original sparse matrix
        full_sparse = SMat;
    end
    
    % Write the matrix to Harwell-Boeing format
    % Assuming you're using the standard `writehb` function (common in MATLAB toolboxes)
    writehb(fileName, full_sparse, title, key, type);
end

Key Details to Note:

  • Placeholder Values: Using ones as a temporary value ensures explicit zero positions are retained in the sparse matrix structure—MATLAB's sparse function only keeps entries with non-zero values. We overwrite those positions back to zero once the matrix is built.
  • Overlap Check: The optional overlap mask prevents accidentally overwriting non-zero entries with zeros, which is critical if your zerosMat might include positions that already have non-zero values in SMat.
  • HB Format Export: The writehb function is the standard tool for exporting to Harwell-Boeing format in MATLAB—ensure you have the necessary toolbox (like Optimization Toolbox) or a compatible third-party implementation installed.

This method guarantees that all explicit zero positions are preserved in the final sparse matrix, which is essential for accurate conversion to formats that require retaining these entries.

内容的提问来源于stack exchange,提问作者Nawal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:05:38