转载: https://blog.csdn.net/aimatfuture/article/details/7741892
function [He Xx] = GQR(A) % param A : 是一个可逆矩阵 % return He : 是一个满足每两个不同列向量的内积为0的矩阵 % return Xx : 是一个主对角线全为1的上三角矩阵 % function : 分解 A = He * Xx % CopyRight : edu.nust.cs726.JunH(111060881) [H L] = size(A); if H ~= L error('hehehe'); end if det(A) == 0 error('hahaha'); end BIJ = eye(H); B = zeros(H, L); B(:, 1) = A(:, 1); for R = 2:L ar = A(:, R); br = ar; for K = 1:R-1 % disp(ar) % disp(B(:, K)); % disp(' '); % disp(' '); bij = Gb(ar, B(:, K)); BIJ(K, R) = bij; br = br - bij*B(:, K); end B(:, R) = br; end He = B; Xx = BIJ; function bij = Gb(aierfa, beita) if 1 == size(aierfa, 1) && 1 == size(beita, 1) bij = (aierfa*beita') / (beita*beita'); else bij = (aierfa'*beita) / (beita'*beita); end
----------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
import sys, os import json import traceback import time import csv import numpy as np from PIL import Image import math # https://blog.csdn.net/aimatfuture/article/details/7741892 # http://blog.sina.com.cn/s/blog_bb81c2230102xe1u.html def getScalue(a, b, bandSize): bb =0.0 ab = 0.0 for i in range(bandSize): ab = ab + a[i]*b[i] bb = bb + b[i]*b[i] scale = ab / bb return scale def gramSchmidt( rasterData, bandCount, bandSize, R): for i in range(bandCount): for j in range(bandCount): R[i][j] = 1 if (i == j) else 0 # column = [0] * bandSize for i in range(1,bandCount): for j in range(0, bandSize): column[j] = rasterData[i][j] # for j in range(0, i): scaleValue = getScalue(column, rasterData[j], bandSize) R[j][i] = scaleValue for k in range(0, bandSize): column[k] = column[k] - scaleValue * rasterData[j][k] # for j in range(0, bandSize): rasterData[i][j] = column[j] return rasterData def GSModify(U): d, n = U.shape m = min(d, n) R = np.eye(m, n) Q = np.zeros((d, m), dtype=np.float32) D = np.zeros(m, dtype=np.float32) for i in range(0, m): if i == 0: e = U[:, i] Q[:, i] = e D[i] = np.dot(e, e.T) else: a = Q[:, 0:i] # Q[:, 0:i-1] b = 1 / D[0:i] # 1 / D[0:i-1] c = np.multiply(a, b) d = c.T # .reshape((height*width, i)) e = U[:, i] f = np.dot(d, e) R[0:i, i] = f g = np.dot(a, f) h = (e-g) Q[:, i] = h k = np.dot(h, h.T) D[i] = k return Q, R rasterData1 = [ [1, 2, -1], [-1,3,1], [4,-1,0]] R1 = [ [0, 0, 0], [0, 0, 0], [0, 0, 0]] Q1 = gramSchmidt( rasterData1, 3, 3, R1) print(R1) print(Q1) print('---------------------------------------------------------------') panDataset = np.array([1, 2, -1, -1,3,1, 4,-1,0]) rasterData2 = panDataset.reshape((3, 3), order='F') Q2, R2 = GSModify(rasterData2) print(rasterData2) print(R2) print(Q2)
----------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------