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 commentaires

Massimo Zanetti
Massimo Zanetti le 29 Sep 2016
Modifié(e) : Massimo Zanetti le 29 Sep 2016
It important to know how you want to store also the other points.. Are you assuming that the final B matrix will be 3 times tall than A?
Something like this
A = [ x1, x2, x3;
y1, y2, y3;
...
z1, z2, z3];
B = [ x1, x2;
x2, x3;
x3, x1;
y1, y2;
y2, y3;
y3, y1;
....
z1, z2;
z2, z3;
z3, z1];
??
Meghan
Meghan le 29 Sep 2016
My apologies. Basically I want to use the data from matrix A to form matrix B. So in doing that B would become a nx2 matrix. I'm unsure how many rows B would have. Again A will change dimensions in the future, with more or less rows so I can't be too definitive on matrix structure. Its seems like a really simple thing to do but I've been trying to do it for the last couple of days and I'm running out of ideas!
KSSV
KSSV le 29 Sep 2016
What is the logic behind converting 40X3 to 40X2?
Meghan
Meghan le 29 Sep 2016
It won't be 40x2.
I am extracting the vertices of each triangle. A has three nodal points per row, giving a triangle. I need B to give the nodal points associated with the vertices of the triangles. So B will probably be 120x2 in this scenario.
This is just a small fraction of what I have to do for a larger project but without it I will not be able to continue. I am trying to identify boundary nodes and define whether they are land, sea or internal nodes. This is why I need the vertices.
KSSV
KSSV le 29 Sep 2016
Modifié(e) : KSSV le 29 Sep 2016
Okay..you are dealing with unstructured mesh, you have coordinates and nodal connectivity data. Can't you get this information from nodal connectivity data? As you want the respective nodal points of vertices in A, won't B have three columns?
Meghan
Meghan le 29 Sep 2016
Modifié(e) : Meghan le 29 Sep 2016
No B will not have 3 columns. It will have 2. Each column will have a nodal point. Those nodal points are attached via a vertex, associated with a triangle. I'm not using coordinates. I'm using node IDs.
For example:
A=
13972 13589 13971
13971 14310 14311
13972 13971 14311
14312 13972 14311
13972 14312 13973
13973 14312 14313
13973 14313 14314
14310 14634 14311
14635 14311 14634
14311 14635 14312
Each row contains three nodal points, which make the triangle. So in this case, take the first row of A and it gives:
B=
13972 13589
13589 13971
13971 13972
That gives each vertex of the triangle, which is why B would be about 120x2. Obviously I'd be able to just write it all out, but that takes time. It also isn't helpful because I'll have to run this code in different areas, using different nodes and different dimensions. So ideally I need a way to do it so that no values are mentioned, in something like a for loop.
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. The matrix
C = A(:,ind)
concatenates the columns of A in the correct order to make the pairs you want, its first row being 13972 13589 13589 13971 13971 13972 but 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 with ':
B = reshape(C',2,120)'
Image Analyst
Image Analyst le 29 Sep 2016
Attaching a diagram or screenshot would help us visualize things.
not sure if previous comment is addressing my comment, but
A =
4 5 6
10 11 12
22 23 24
36 37 38
>> ind = [1 2 2 3 3 1]; C = A(:,ind)
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
>> C'
ans =
4 10 22 36
5 11 23 37
5 11 23 37
6 12 24 38
6 12 24 38
4 10 22 36
>> D = reshape(C',2,12) % reads C' out columnwise
D =
4 5 6 10 11 12 22 23 24 36 37 38
5 6 4 11 12 10 23 24 22 37 38 36
>> B = D'
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
Meghan
Meghan le 30 Sep 2016
Modifié(e) : Meghan le 30 Sep 2016
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 le 30 Sep 2016
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.

Connectez-vous pour commenter.

 Réponse acceptée

David Goodmanson
David Goodmanson le 30 Sep 2016
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.

Plus de réponses (1)

Andrei Bobrov
Andrei Bobrov le 30 Sep 2016
Modifié(e) : Andrei Bobrov le 30 Sep 2016
A = [...
4 5 6
10 11 12
22 23 24
36 37 38]
B = reshape([A.',circshift(A,[0,-1]).'],[],2);

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by