필터 지우기
필터 지우기

How to extract vertices from a matrix.

조회 수: 9 (최근 30일)
Meghan
Meghan 2016년 9월 29일
답변: David Goodmanson 2016년 9월 30일
Hi everyone
I wonder if you can help me with a problem I'm having.
Right now I have a matrix which is 40x3 and contains nodal points related to triangles making each row a triangle. I'm trying to make a different matrix which would just contain two columns, with the nodal points associated with a vertex. For example:
B(1,1)=A(1,1);
B(1,2)=A(1,2);
B(2,1)=A(1,2);
B(2,2)=A(1,3);
B(3,1)=A(1,3);
B(3,2)=A(1,1);
Now I could continue doing it that way to make the matrix I want, but this code will be used many times and the dimensions of the matrices will change so I've been trying to find a way to do it in a for loop, or something of the sort.
Any help would be much appreciated :)
  댓글 수: 11
Meghan
Meghan 2016년 9월 30일
편집: Meghan 2016년 9월 30일
That worked! Thank you so much :) You're an absolute star! It seems really quite simple, I think I was making it a lot more complex in my head than it needed to be!
p.s. If you put write it as an answer I'll accept it :)
David Goodmanson
David Goodmanson 2016년 9월 30일
I'm glad you liked this solution and I will post it as an answer. Thank you for mentioning that idea.
The great thing about Matlab is that its syntax simplifies things compared to 'for' loops and such, and you can look at matrix calculations in an almost pictorial way.

댓글을 달려면 로그인하십시오.

채택된 답변

David Goodmanson
David Goodmanson 2016년 9월 30일
Suppose you create an index vector
ind = [1 2 2 3 3 1]
which is the same as your column index for A in your original posting. Then for the sample matrix
A =
4 5 6
10 11 12
22 23 24
36 37 38
the command
C = A(:,ind)
concatenates the columns of A in the correct order to make the pairs you want:
C =
4 5 5 6 6 4
10 11 11 12 12 10
22 23 23 24 24 22
36 37 37 38 38 36
but for matrix B the pairs need to be stacked on top of each other. The 'reshape' command will do this but it reads elements out columnwise, so it's necessary to do some transposing back and forth using the single quote operator:
B = reshape(C',2,120)'
B =
4 5
5 6
6 4
10 11
11 12
12 10
22 23
23 24
24 22
36 37
37 38
38 36
The comments section for this question shows some intermediate matrices in this process.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2016년 9월 30일
편집: Andrei Bobrov 2016년 9월 30일
A = [...
4 5 6
10 11 12
22 23 24
36 37 38]
B = reshape([A.',circshift(A,[0,-1]).'],[],2);

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by