Matlab, structural matrix form with matrices oriented correctly

I have a structure called poseSets, and it contains two things:

  • Pose
  • Time

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

So, let's imagine that I have a list of structures whose length is 10. I can get almost my list by doing the following:

 [structList.Pose] 

But this gives me a matrix (4xN) x 4 , i.e.

 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 I really want 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 can not transpose it, because each of the matrices will be individually transposed and will be in the wrong way.

Now you can solve this with the 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 one liner or the best way to do this?

+4
source share
3 answers

Yes, I also find this rather annoying ... some things in Matlab don't seem consistent in terms of row significance or column significance. This is one example where everything is concatenated differently (= row-major), while the vast majority of algorithms have columns. linspace or common ranges (e.g. x = 0:5:100 ) are another striking example of generating a row matrix, while x(:) then again the main column ... ¯ \ (° _ °) / ¯

In any case, the easiest way to resolve is to force the column to concatenate:

 cat(1, structList.Pose) 
+6
source

try the following: vertcat(structList.Pose)

+3
source

This can be done, but I am sure that this is not one-line:

 % generate some data M = magic(4) poseSets = struct('pose',M); poseSets = repmat(poseSets,3,1) poseList = cellfun(@transpose, {poseSets.pose}, 'UniformOutput', false); poseList = [poseList{:}].' 
+1
source

Source: https://habr.com/ru/post/1445698/


All Articles