split mtatrix and name automatically
이전 댓글 표시
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
Walter Roberson
2022년 11월 5일
Yes it is possible to do that with a for loop, naming the arrays the way you ask. However we need to ask why you are doing that? What reason do you have for splitting into separate variables instead of just indexing the single variable?
mehra
2022년 11월 5일
"the reason I need to split is that later I use a function which uses (n,1) matrixes, since my matrix is (n,15) I need to split it."
Nothing in your explanation requires dynamically named variables. Your suggested approach would be slow, complex, inefficient, insecure, and obfuscate the code intent:
Everything you have explained so far would be simpler and much more efficient using standard approaches e.g. indexing into a cell array or 3D array. Your question is a good example of this:
mehra
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?
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!