split mtatrix and name automatically
1 次查看(过去 30 天)
显示 更早的评论
Hello guys
I have a mat file (attached) which is a 10004*15 matrix. I need to split it into 15 seprated matrixes like (10004*1). and I need to name these matrixes like ur1, ur2, ...ur15.
I know that ı can split matrixes by using the following code:
ur1=ur(:,15);
ur2=ur(:,14)
% and so on....
but I want to know if I can use it in a for loop so that I can have fewer lines (since I need to use the same code for other data files and it will be so long).
Also I want to ask if there is a way to name my output matrixes automatically.
5 个评论
Stephen23
2022-11-5
编辑:Stephen23
2022-11-5
"... I need to name these matrixes like ur1, ur2, ...ur15."
If the code requires lots of numbered variable names then you are doing something wrong.
Using indexing is simpler, neater, easier, and much more efficient than your approach. What is stopping you from using the indexing directly, or a simple cell array?
采纳的回答
the cyclist
2022-11-5
编辑:the cyclist
2022-11-5
Anywhere that you could have used the variable name ur1, you can almost always just reference ur(:,1) directly instead, and not used the dynamically named variables. (Please read this post about why dynamically named variables are a terrible idea in general.)
If you must break out into individual variables, then using a cell array is typically a better idea:
% Initialize empty cell array
urc = cell(15,1);
% Fill them
for nc = 1:15
urc{nc} = ur(:,nc); % Note the use of curly brackets to denote contents of cell. See docs.
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!