2
A
回答
3
您可以看到,矩陣的右上部分由1/sqrt(n*(n-1))
組成,對角元素由-(n-1)/sqrt(n*(n-1))
組成,第一列由1/sqrt(n)
組成,其餘元素爲零。
我們可以生成完整的矩陣,它包含第一列全部爲1/sqrt(n)
,然後剩下的列爲1/sqrt(n*(n-1))
然後我們需要修改矩陣以包含剩餘部分。
因此,讓我們專注於從第2行第2列開始的元素,因爲它們遵循一個模式。一旦完成,我們可以構建其他構建最終矩陣的東西。
x = 2:n;
Hsmall = repmat([1./sqrt(x.*(x-1))], n-1, 1);
下一步,我們將解決對角線元素:
Hsmall(logical(eye(n-1))) = -(x-1)./sqrt(x.*(x-1));
現在,讓我們零元素的其餘部分:
Hsmall(tril(logical(ones(n-1)),-1)) = 0;
既然我們已經做了,讓我們創建一個將所有這些組合在一起的新矩陣:
H = [1/sqrt(n) 1./sqrt(x.*(x-1)); repmat(1/sqrt(n), n-1, 1) Hsmall];
因此,完整的代碼:
x = 2:n;
Hsmall = repmat([1./sqrt(x.*(x-1))], n-1, 1);
Hsmall(logical(eye(n-1))) = -(x-1)./sqrt(x.*(x-1));
Hsmall(tril(logical(ones(n-1)),-1)) = 0;
H = [1/sqrt(n) 1./sqrt(x.*(x-1)); repmat(1/sqrt(n), n-1, 1) Hsmall];
下面是與n = 6
一個例子:
>> H
H =
Columns 1 through 3
0.408248290463863 0.707106781186547 0.408248290463863
0.408248290463863 -0.707106781186547 0.408248290463863
0.408248290463863 0 -0.816496580927726
0.408248290463863 0 0
0.408248290463863 0 0
0.408248290463863 0 0
Columns 4 through 6
0.288675134594813 0.223606797749979 0.182574185835055
0.288675134594813 0.223606797749979 0.182574185835055
0.288675134594813 0.223606797749979 0.182574185835055
-0.866025403784439 0.223606797749979 0.182574185835055
0 -0.894427190999916 0.182574185835055
0 0 -0.912870929175277
1
既然你用的10000
一個相當大的n
價值的工作,你可能想擠出儘可能多的表現儘可能。 與展望,您可以使用基於cumsum
的有效途徑 -
%// Values to be set in each column for the upper triangular region
upper_tri = 1./sqrt([1:n].*(0:n-1));
%// Diagonal indices
diag_idx = [1:n+1:n*n];
%// Setup output array
out = zeros(n,n);
%// Set the first row of output array with upper triangular values
out(1,:) = upper_tri;
%// Set the diagonal elements with the negative triangular values.
%// The intention here is to perform CUMSUM across each column later on,
%// thus therewould be zeros beyond the diagonal positions for each column
out(diag_idx) = -upper_tri;
%// Set the first element of output array with n^(-1/2)
out(1) = -1/sqrt(n);
%// Finally, perform CUMSUM as suggested earlier
out = cumsum(out,1);
%// Set the diagonal elements with the actually expected values
out(diag_idx(2:end)) = upper_tri(2:end).*[-1:-1:-(n-1)];
運行測試
(i)與n = 10000
,在我結束運行時是 - Elapsed time is 0.457543 seconds
。 (II)現在,作爲最終的性能壓縮練習,您可以使用此MATLAB Undodumented Blog中列出的更快的預分配方案來編輯out
的預分配步驟。因此,預分配步驟將如下所示 -
out(n,n) = 0;
此編輯代碼的運行時爲 - Elapsed time is 0.400399 seconds
。
(三)n = 10000
與other answer by @rayryeng運行時產生 - Elapsed time is 1.306339 seconds.
相關問題
- 1. 矩陣生成MATLAB
- 2. Matlab生成矩陣
- 3. 如何乘這個矩陣在MATLAB
- 4. 從矩陣生成的矩陣與Matlab生成的Python圖形
- 5. 如何加載這種矩陣在MATLAB
- 6. 如何用matlab生成下一個值的矩陣?
- 7. 創建這個矩陣在MATLAB
- 8. Matlab - 爲矩陣生成隨機座標
- 9. Matlab的遞歸函數生成矩陣
- 10. 如何生成矩陣?
- 11. 如何生成矩陣?
- 12. 如何在matlab/octave中生成大型矩陣?
- 13. 如何在Matlab矩陣
- 14. 生成矩陣
- 15. 如何在matlab parfor循環中切分這個矩陣?
- 16. 如何在MATLAB中做這個對角矩陣?
- 17. 在MATLAB矩陣
- 18. Matlab - 通過循環生成多個矩陣
- 19. 給定一個數,用MATLAB生成一系列'L'形矩陣
- 20. 如何均衡matlab中這兩個矩陣的大小?
- 21. 如何解決這個通用的Matlab矩陣技巧
- 22. cellarray在矩陣matlab
- 23. 矩陣矩陣在Matlab中的數組?
- 24. 如何生成循環矩陣?
- 25. 如何生成組合矩陣
- 26. 如何生成以下矩陣?
- 27. 如何通過循環生成矩陣?
- 28. 如何生成對稱隨機矩陣?
- 29. Matlab - 矩陣陣列
- 30. 在Matlab中生成矩陣的所有可能組合
謝謝,我學到了一些東西。 – aaa
@meng - 很高興能幫到你!這是有趣的問題。 – rayryeng
一如既往的好解釋!本來會跟triu + repmat一起去的,所以不得不尋找替代方法:) – Divakar