Reshape a Matrix in a specific order

7 views (last 30 days)
Hello everyone, i have a 36x3 Matrix where every row represents the coordinates of a corner point of a triangle (x,y,z). So every three rows form a triangle. What i need is a 12x9 Matrix where every row represents a triangle (x1,y1,z1,x2,y2,z2,x3,y3,z3) Since reshape dosent work i tried using a for loop. This is how it would be if it was only one row
vert(1,1:3) = v(1,:)
vert(1,4:6) = v(2,:)
vert(1,7:9) = v(3,:)
And this is the for loop i have written:
for i = 1:nfaces
count = 3:nvert;
for j = count-2:nvert
for k = 1:3
for l = 1:9
vert(i,l) = v(j,k)
end
end
count+3;
end
end
nfaces is the total number of triangles i have and nvert is the number of corner points. In the end i get the 12x9 matrix but it seems that with every loop every element gets overwritten. e.g in the end all my elements are 50. i guess that i have to put a statement outside the loop but i havent figured out how to do it. I also tried other loops without the "count" but the result remains the same. I would appreciate it if someone could help me out and show me where i am wrong. Best regards, Markus

Accepted Answer

Ameer Hamza
Ameer Hamza on 3 May 2018
Edited: Ameer Hamza on 3 May 2018
"Since reshape dosent work"
reshape(matrix', 9, [])'
  2 Comments
Markus Kucera
Markus Kucera on 4 May 2018
Edited: Markus Kucera on 4 May 2018
OKay now i feel a little bit stupid, it seems like i didnt quite understand how reshape works... But thanks for your help, i really appreciate it.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 3 May 2018
Do you need the result to be a matrix of all the coordinates of the vertices of the triangles, or would you be able to work with the matrix of coordinates and a connectivity matrix? The triangulation object uses the latter approach, storing the coordinates in its Points property and the connectivity matrix in its ConnectivityList property. Based on your description, creating the connectivity matrix should be fairly straightforward.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!