It is very easy to collapse several matrices (of differing sizes) into a single 1xN vector:
vec = [ A(:)', B(:)', C(:)' ];
... but how can I now go the other way, i.e. recover U,V,W from vec?
I could collect the sizes of A, B, C:
sizes = [ size(A), size(B), size(C) ];
... but I can't see any clean way of doing the recovery.
k=0;
U = reshape( vec(k+1:k+Ay*Ax), Ay, Ax); k = k+Ay*Ax;
V = reshape( vec(k+1:k+By*Bx), By, Bx); k = k+By*Bx;
W = reshape( vec(k+1:k+Cy*Cx), Cy, Cx); k = k+Cy*Cx;
eeeeeyYUCK! Surely there has to be something better than this?
EDIT: In response to CST-link's question, @CST-Link, I actually have 7 objects so I don't need to be overly generic. However I'm actually dealing with the following structure:
A = ...; B = ...; C = ...; % only 7 mats
vec = pack(A,B,C);
ret = f( vec, g );
A_, B_, C_ = unpack(ret);
function ret = g( vec )
U, V, W = unpack(vec);
% fiddle U V W
ret = pack(U,V,W);
end
... and f will invoke g(vec).
In order to perform the inner unpack, I would need to feed 7 target dimensions, so I guess I'm going to have to send that data in as a separate param:
A = ...; B = ...; C = ...; % only 7 mats
vec = pack(A,B,C); sizes = getsizes(A,B,C);
ret = f( vec, g, sizes );
A_, B_, C_ = unpack(ret);
function ret = g( vec, sizes )
U, V, W = unpack(vec, sizes);
% fiddle U V W
ret = pack(U,V,W);
end
... although the unpack
function can still access A, B, C, so actually it would be tidier not to mess around with that extra sizes
variable.