I would like to create a column vector from the elements of a matrix A
of size (3,3)
that are not on the diagonal. Thus, I would have 6
elements in that output vector. How can I do this?

- 102,964
- 22
- 184
- 193

- 63
- 1
- 6
-
Made few edits to make it more clear, please cross check to make sure that it explains your problem well enough. – Divakar Oct 31 '14 at 16:19
4 Answers
Use this to get such a column vector, assuming A
is the input matrix -
column_vector = A(eye(size(A))==0)
If you don't care about the order of the elements in the output, you can also use a combination of setdiff
and diag
-
column_vector = setdiff(A,diag(A))

- 218,885
- 19
- 262
- 358
You can also use linear indexing to access the diagonal elements and null them. This will automatically reshape itself to a single vector:
A(1:size(A,1)+1:end) = [];
Bear in mind that this will mutate the original matrix A
. If you don't want this to happen, make a copy of your matrix then perform the above operation on that copy. In other words:
Acopy = A;
Acopy(1:size(A,1)+1:end) = [];
Acopy
will contain the final result. You need to create a vector starting from 1 and going to the end in increments of the rows of the matrix A
added with 1
due to the fact that linear indices are column-major, so the linear indices used to access a matrix progress down each row first for a particular column. size(A,1)
will allow us to offset by each column and we add 1
each time to ensure we get the diagonal coefficient for each column in the matrix.

- 102,964
- 22
- 184
- 193
-
1+1 for the Acopy version! I think that was crucial, because OP wanted a vector *from* the 2D matrix. – Divakar Oct 31 '14 at 17:50
-
@Divakar - Thanks :) I +1ed you too. I realized that I mutated the original matrix, which is why I revised my original post. I didn't read the question very carefully! – rayryeng Oct 31 '14 at 17:52
-
1Cool idea to use matrix stride+1 as the removal index to avoid making a full size logical! +1 – chappjc Oct 31 '14 at 17:56
-
Use eye
and logical negation, although this is no better than Divakar's original answer, and possibly significantly slower for very large matrices.
>> A = magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> A(~eye(size(A)))
ans =
5
9
4
2
7
14
3
10
15
13
8
12

- 30,359
- 6
- 75
- 132
-
I thought about this, but then I remembered that this would be mostly slower than `==0` [Is A==0 really better than ~A?](http://stackoverflow.com/questions/25339215/is-a-0-really-better-than-a), so I went with `==0`. – Divakar Oct 31 '14 at 17:51
-
I +1ed anyway because it's still a valid solution... and the fact that I'm happy you're back :) – rayryeng Oct 31 '14 at 17:52
-
@Divakar Good info, and good previous question. I hadn't noticed that... must have been during my sabbatical. ;) – chappjc Oct 31 '14 at 17:53
-
It all started from [this simple question](http://stackoverflow.com/questions/25321341/find-the-number-of-zero-elements-in-a-matrix-in-matlab) as we were doing some bechmarks. +1 already for effort I guess ;) – Divakar Oct 31 '14 at 17:58
-
@Divakar Haha! I've seen that question and downvoted the accepted answer (come on, `length`?). Quite amusing! Good effort on that answer, by the way, I'd vote twice if I could... no detail is too small when you're going for performance. – chappjc Oct 31 '14 at 18:01
-
1@chappjc - It's a funny story... that question poster as well as the person who answered it were in cahoots with each other. One of them would post a question and 20 seconds later the other person would answer and it got accepted. That stuff doesn't bother me, but the fact that the answers were very poor quality bugged me (i.e. using `length`). I put up an inquiry on Meta a while ago: http://meta.stackoverflow.com/questions/268821/possible-reputation-padding ... to see what the community thought. Suffice it to say that after asking the community, he never answered any questions again. Oops. – rayryeng Oct 31 '14 at 18:07
-
1@rayryeng Wow, that kind of s%^T really pisses me off. Amazing that people fake it. What's the point? It just minimizes other people's honest efforts. – chappjc Oct 31 '14 at 18:08
-
@chappjc - I totally agree. Divakar's and natan's solutions were much more elegant. Doing `find` nested with `length` has too much computational overhead. It amazed me as to how that got accepted. I probably should have flagged it privately and brought it up with the moderators, but a part of me wanted him to stop posting garbage on StackOverflow. – rayryeng Oct 31 '14 at 18:13
-
@Divakar - Thanks :) I originally brought it up with the community because I found that immediate question-answer pattern a bit peculiar.... but then when I thought about it, I hated the quality of the answers above anything. If the answers had excellent quality, I wouldn't have thought twice about it. – rayryeng Oct 31 '14 at 18:14
-
@GhazwanAlsoufi Thanks. Please upvote my answer since you found it helpful. – chappjc Apr 16 '15 at 17:11
Assuming that the matrix is square,
v = A(mod(0:numel(A)-1, size(A,1)+1) > 0).';

- 110,752
- 13
- 76
- 147