How to transpose many matrices X_1 ... X_N at once?
3 views (last 30 days)
Show older comments
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
1 Comment
Answers (4)
Star Strider
on 23 Aug 2022
9 Comments
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.
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{:}
Y=cellfun(@transpose, X, 'uni',0); Y{:}
1 Comment
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
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
10 Comments
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
See Also
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!