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中用另一个矩阵生成随机矩阵
- 5. 从矩阵生成的矩阵与Matlab生成的Python图形
- 6. Matlab的生成多个随机矩阵
- 7. 如何在Matlab上生成两个矩阵的平均值?
- 8. 如何加载这种矩阵在MATLAB
- 9. 如何用matlab生成下一个值的矩阵?
- 10. 创建这个矩阵在MATLAB
- 11. 矩阵1,2,3如何生成?
- 12. 如何生成矩阵?
- 13. 如何生成矩阵?
- 14. 如何生成矩阵?
- 15. MATLAB函数替换randi生成矩阵
- 16. Matlab的递归函数生成矩阵
- 17. Matlab - 为矩阵生成随机坐标
- 18. 从另一个矩阵生成矩阵
- 19. 如何在matlab/octave中生成大型矩阵?
- 20. 如何在Matlab矩阵
- 21. 生成矩阵
- 22. 生成矩阵
- 23. 如何在matlab中生成一个随机矩阵,每个值重复两次?
- 24. 在matlab中生成每个二进制n×m矩阵
- 25. 如何在matlab中将矩阵块连接到单个矩阵?
- 26. 如何在matlab parfor循环中切分这个矩阵?
- 27. 如何在MATLAB中做这个对角矩阵?
- 28. 如何在matlab中迭代这个矩阵?
- 29. 在MATLAB矩阵
- 30. 从matlab中的另一个矩阵产生布尔矩阵
谢谢,我学到了一些东西。 – aaa
@meng - 很高兴能帮到你!这是有趣的问题。 – rayryeng
一如既往的好解释!本来会跟triu + repmat一起去的,所以不得不寻找替代方法:) – Divakar