How do I create a vector array out of certain values of a matrix?
52 ビュー (過去 30 日間)
古いコメントを表示
Hi all,
I have a 76x76 matrix, and I want to make the matrix into a vector array instead. But, the matrix is a symmetric matrix, and I only want the non repeated values. So basically, I only want the values from the top right triangle, excluding the diagonal line of value(which are all 1s). So something like i = (1:75), and j = (i+1:76). I am not too sure how to write this in a function though, so please help.
Thank you very much!
0 件のコメント
回答 (2 件)
Guillaume
2016 年 8 月 23 日
m = reshape(1:76*76, 76, 76); %demo matrix
m(triu(true(size(m)), 1))
7 件のコメント
sai kumar s
2022 年 12 月 31 日
along with the componenets in the upper triangular matrix i need diagonal componenets also. how can i extend the above code.
Voss
2023 年 1 月 1 日
U = triu(X,k) returns the element on and above the kth diagonal of X
So, triu(X,0) returns all the elements above the main diagonal, with the main diagonal.
X = magic(6)
triu(X,0)
Robert
2016 年 8 月 23 日
Using some example data, here is the code using logical indexing to grab all the values of the matrix whose column index is greater than its row index (upper triangle not including the diagonal).
n = 76;
x = magic(n);
[ii,jj] = meshgrid(1:n,1:n);
x(ii>jj)
1 件のコメント
Guillaume
2016 年 8 月 23 日
Note that because you're using meshgrid instead of ndgrid, ii is actually the column indices, and jj the row indices. To make that clearer and avoid future bugs when the matrix is not square, I'd use:
[icols, irows] = meshgrid(1:size(x, 2), 1:size(x, 1));
%or
[irows, icols] = ndgrid(1:size(x, 1), 1:size(x, 2));
Or use triu to build the logical matrix directly.
参考
カテゴリ
Help Center および File Exchange で Operating on Diagonal Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!