How do I create a vector array out of certain values of a matrix?

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!

Respuestas (2)

The triu function can be used to create the logical matrix for the mask you want, so:
m = reshape(1:76*76, 76, 76); %demo matrix
m(triu(true(size(m)), 1))

7 comentarios

Hi, Thanks for your reply.
Doesn't triu return the upper triangular part? I don't want the diagonal line though which it includes doesn't it?
As per the documentation that I linked:
U = triu(X,k) returns the element on and above the kth diagonal of X
So, triu(X, 1) returns all the elements above the main diagonal, without the main diagonal.
ah yes! k = 0 is the main diagonal. Thank you.
I tried the code, but it didn't seem to go right. Do I need to change the numbers inside? I'm not too sure what each of them are for. thanks for your help!
Lakyn
Lakyn el 23 de Ag. de 2016
Editada: Guillaume el 24 de Ag. de 2016
So I wrote a function off that as follows:
function L = vector(m)
m = reshape(1:76*76, 76, 76); %demo matrix
m(triu(true(size(m)), 1))
And then I ran it on the command window with my matrix, and the values I got were basically numbers in ascending order to 5775 instead of my data in the matrix. Did I write something wrong?
As per the comment, m = ... is just creating a matrix for demonstration. Of course, if you overwrite the original matrix by the demo matrix, the output is going the numbers in the demo matrix.
function L = vector(m) %better variable names and function name advised
L = m(triu(true(size(m)), 1));
end
along with the componenets in the upper triangular matrix i need diagonal componenets also. how can i extend the above code.
@sai kumar s: As per the documentation that @Guillaume linked:
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)
X = 6×6
35 1 6 26 19 24 3 32 7 21 23 25 31 9 2 22 27 20 8 28 33 17 10 15 30 5 34 12 14 16 4 36 29 13 18 11
triu(X,0)
ans = 6×6
35 1 6 26 19 24 0 32 7 21 23 25 0 0 2 22 27 20 0 0 0 17 10 15 0 0 0 0 14 16 0 0 0 0 0 11

Iniciar sesión para comentar.

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 comentario

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.

Iniciar sesión para comentar.

Preguntada:

el 23 de Ag. de 2016

Comentada:

el 1 de En. de 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by