1

I have a text file with points coordinates.

X1 Y1
X2 Y2
X3 Y3
...
Xn Yn

The idea is to build a matrix of this coordinates.

I coded a first version that reads the X and Y values, push them into respectively @Xs and @Yx array, and then create the matrix using

$Points = PDL::Matrix->pdl([\@Xs, \@Ys]);

I then get a 2 x n Matrix :

[
 [X1, X2, X3, .., Xn]
 [Y1, Y2, Y3, .., Yn]
]

I now would like to build a n x 2 Matrix instead :

[
 [X1, Y1]
 [X2, Y2]
 ...
 [Xn, Yn]
]

but I don't see how I can do it as the push fonction seems not to exist for PDL::Matrix

Thank you for your help,

EDIT 1 :

Here is what I would like to do :

sub GetCoordinates {
  my (@hl) = @_;
  my $Point;
  my $Points;

  foreach my $v (@hl) 
  {
    my %values = ($v =~ m/${regex_Coordinates}/g);

    if ($values{X} && $values{Y}) 
    {
        $Point = mpdl [$values{X},$values{Y}];
        push($Points, $Point);                  # DOES NOT EXIST
    }
  }
}
Alexglvr
  • 427
  • 5
  • 18

2 Answers2

3

You can call

$m->transpose

on the 2×n matrix.

choroba
  • 231,213
  • 25
  • 204
  • 289
  • Thank you for your answer. Yes your right, but just to know, is there a solution around making vectors [Xi, Yi] and pushing them into a matrix as row? – Alexglvr Feb 19 '19 at 19:24
  • Can you include the code used to populate the matrix into the question? – choroba Feb 19 '19 at 19:35
  • any idea? In my programme strcture, It would really help to be able to push vectors into a matrix... – Alexglvr Feb 21 '19 at 09:47
1

The choroba answer works. Here is another solution that works too and which fits perfectly my needs

my @Points

file read loop {
 push(@Points, vpdl [$PointX, $PointY]);
}

my $PointsMatrix = PDL::Matrix->pdl(\@Points); #nx2 Matrix

If it can Help !

Alexglvr
  • 427
  • 5
  • 18