How to transpose many matrices X_1 ... X_N at once?

3 views (last 30 days)
JJJ
JJJ on 23 Aug 2022
Edited: Stephen23 on 24 Aug 2022
My question is very simple. I have the objects X_1 X_2 ... X_N. I want to get all of their transposes:
Y_1 = X_1';
...
Y_N = X_N';
But when I try to use for loop like
for t = 1:10
Y_t = X_t';
end
It doesn't work. How do I most easily achieve what I am trying to do? Thanks

Answers (4)

Star Strider
Star Strider on 23 Aug 2022
Try something like this —
X1 = randi(9,3)
X1 = 3×3
9 1 6 9 8 7 3 6 8
X2 = randi(9,3)
X2 = 3×3
5 1 2 6 9 3 5 8 2
X3 = randi(9,3)
X3 = 3×3
6 3 6 6 7 8 3 6 2
Xcat = cat(3,X1,X2,X3);
Ycat = permute(Xcat,[2 1 3]);
Y1 = Ycat(:,:,1)
Y1 = 3×3
9 9 3 1 8 6 6 7 8
Y2 = Ycat(:,:,2)
Y2 = 3×3
5 6 5 1 9 8 2 3 2
Y3 = Ycat(:,:,3)
Y3 = 3×3
6 6 3 3 7 6 6 8 2
See the documentation on cat and permute to usnerstand how it works.
.
  9 Comments
Stephen23
Stephen23 on 23 Aug 2022
Edited: Stephen23 on 23 Aug 2022
"And that I would have the fill the ... myself. Right?"
In fact the best approach by far is to avoid getting into this situation entirely. The point where you created those variables is the point where you should fix your code, not later by making a cell array by laboriously listing all of those variable names. No one here would do that.
"I guess this is as minimal as it gets."
No, better data design easily allows you to write simpler, more efficient code. For example, if you are LOADing data from MAT files always LOAD into an output variable rather than durectly into the workspace:
S = load(..)
Your data design (forcing meta-data into variables names) makes your code complex, slow, and inefficient.
Solution: better data design using one container array.
JJJ
JJJ on 24 Aug 2022
Edited: JJJ on 24 Aug 2022
@Stephen23 please check my latest comment.
@Star Strider I think this is the best approach.

Sign in to comment.


Matt J
Matt J on 23 Aug 2022
The matrices must be put into some kind of container if you want to operate on them as a group. If you can't cat() them, then they must go in a cell array or struct, e.g.
X={randi(9,2), randi(9,3)}; X{:}
ans = 2×2
3 9 2 3
ans = 3×3
1 1 5 1 3 9 8 5 1
Y=cellfun(@transpose, X, 'uni',0); Y{:}
ans = 2×2
3 2 9 3
ans = 3×3
1 1 8 1 3 5 5 9 1
  1 Comment
Rik
Rik on 23 Aug 2022
I was going to post this as an answer, but then I saw this answer, so I'll post it as a comment instead.
The solution is to use Matlab as it is intended. Clearly the number has a meaning apart from you being able to distinguish Y_1 from Y_5. That means the number is data. You should use indices instead of numbering variables. That will allow you to use a simple loop:
for n=1:numel(X),Y{n}=X{n}.';end

Sign in to comment.


Matt J
Matt J on 23 Aug 2022
Edited: Matt J on 23 Aug 2022
In R2020b or higher, you can use pagetranspose or pagectranspose
X=randi(9,3,3,2)
X =
X(:,:,1) = 1 7 2 4 7 3 6 9 9 X(:,:,2) = 5 8 8 8 6 7 9 4 7
Y=pagetranspose(X)
Y =
Y(:,:,1) = 1 4 6 7 7 9 2 3 9 Y(:,:,2) = 5 8 9 8 6 4 8 7 7

Bruno Luong
Bruno Luong on 23 Aug 2022
This is the solution for the endless issues you are and will be enountered due of your naming variable with counter
X_1 = rand(3),
X_2 = rand(4),
for t = 1:2
eval(sprintf('Y_%d = X_%d''', t, t));
end
Y_1 = 3×3
0.6087 0.0830 0.2248 0.0339 0.9532 0.8557 0.7319 0.8093 0.8381
Y_2 = 4×4
0.7585 0.5014 0.3957 0.9816 0.9527 0.4223 0.5779 0.1252 0.3383 0.6818 0.6281 0.3371 0.7860 0.7223 0.8108 0.6294
  10 Comments
JJJ
JJJ on 24 Aug 2022
Thanks. I get it now. Its as @Star Strider mentioned. Now I import the tomograms as:
load(Tomogram_1,...)
X{1} = Tomogram_1;
and so on.
Then I just use
for t = 1:20
Y{t} = Encode(X{t});
end
If you have further suggestions, I would be glad to know.
Stephen23
Stephen23 on 24 Aug 2022
Edited: Stephen23 on 24 Aug 2022
"If you have further suggestions, I would be glad to know."
You are still using numbered variable names here:
load(Tomogram_1,..) % LOADing directly into the workspace is the rootcause...
X{1} = Tomogram_1; % because you still have numbered variable names!
So you have not really fixed the problem, because you still need to write out all of those names by hand. Do you really want to spend your time writing out variable names? Yesterday I showed you how to avoid this: by LOADing into an output variable. Assuming exactly one variable per file, you could do this:
F = {.. filenames in a cell array ..}
N = numel(F);
X = cell(1,N);
for k = 1:N
X(k) = struct2cell(load(F{k}));
end

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!