设计一个归一化矩阵matlab函数,并给出调用例子
时间: 2024-05-29 17:02:34 浏览: 116
矩阵归一化MATLAB程序.docx
function [normMat] = normalizeMat(mat)
% Normalize a matrix by dividing each element by the sum of all elements
% Input: mat - a matrix
% Output: normMat - the normalized matrix
% Calculate the sum of all elements in the matrix
sumMat = sum(mat(:));
% Divide each element by the sum and create the normalized matrix
normMat = mat./sumMat;
end
% Example usage:
% Normalize a 3x3 matrix
A = [1 2 3; 4 5 6; 7 8 9];
normA = normalizeMat(A);
disp(normA);
% Output:
% 0.0185 0.0370 0.0556
% 0.0741 0.0926 0.1111
% 0.1296 0.1481 0.1667
阅读全文