Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab, struct to matrix form with matrices oriented the correct way

I have a struct called poseSets and it contains two things:

  1. Pose
  2. Time

So what I want to do is to get the poses (Pose is a 4x4 matrix) in to one big long (4xN_Poses) x 4 matrix.

So lets imagine that i have a list of structs that is 10 long. I can get almost my list by doing this:

[structList.Pose]

But this gives me a (4xN) x 4 matrix ie:

1 2 3 4 | 1 2 3 4 | 1 2 3 4 | ...
5 6 7 8 | 5 6 7 8 | 5 6 7 8 | ...
3 5 6 8 | 3 5 6 8 | 3 5 6 8 | ...
0 0 0 1 | 0 0 0 1 | 0 0 0 1 | ...

But what i really want is this:

1 2 3 4
5 6 7 8
3 5 6 8
0 0 0 1
_______ 
1 2 3 4
5 6 7 8
3 5 6 8
0 0 0 1
_______ 
1 2 3 4
5 6 7 8
3 5 6 8
0 0 0 1
_______ 
: : : :

Now i cant transpose it because each of the matrices would be individually transposed and would be the wrong way.

Now you can solve this with a for loop:

poseList = [];

for i = 1:length(PoseSets);
    poseList = [poseList; PoseSets(i).Pose];
end

Note: poseList contains what i want.

But I personally believe that matlab is magic and you should be able to write what you want in english and matlab will deliver. Does anyone know a one liner or a better way to do this?

like image 875
Fantastic Mr Fox Avatar asked Dec 27 '22 14:12

Fantastic Mr Fox


1 Answers

Yes, I also find this quite annoying...some things in Matlab do not seem consistent regarding row-majorness or column-majorness. This is one example where things are concatenated colum-wise (=row-major), while the vast majority of algorithms are column-major. linspace or generic ranges (e.g., x = 0:5:100) are another prime example of row-major matrix generation, while x(:) is then again column-major... ¯\(°_°)/¯

Anyway, the easiest way to resolve is to force column-major concatenation:

cat(1, structList.Pose)
like image 158
Rody Oldenhuis Avatar answered Jan 25 '23 23:01

Rody Oldenhuis