split mtatrix and name automatically

조회 수: 1 (최근 30일)
mehra
mehra 2022년 11월 5일
편집: Stephen23 2022년 11월 5일
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
mehra
mehra 2022년 11월 5일
As I mentioned my goal was to see by using (n,1) matrix in my function the results would be better despiked. I needed to try both to compare the results.
thanks
Stephen23
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
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
  댓글 수: 1
mehra
mehra 2022년 11월 5일
편집: mehra 2022년 11월 5일
Thank you ,
I also rather not using dynamically named variable but previously I used it in that way my despiking function but apparently it did not despiked and my values were still included outliers. It ıs mentioned in the guidance of ''func_despike_phasespace3d'' that fi in [fo, ip] = func_despike_phasespace3d( fi, i_plot, i_opt ) should be (n,1) matrix. So I thought it maybe better to split the matrixes and use them as one-dimensional matrix.
My previous code is like:
for np=1:15;
ur(:,np)=Data.Profiles_VelX(:,np);
vr(:,np)=Data.Profiles_VelY(:,np);
w1r(:,np)=Data.Profiles_VelZ1(:,np);
w2r(:,np)=Data.Profiles_VelZ2(:,np);
[u(:,np), ip1] = func_despike_phasespace3d( ur(:,np),8, 2); %here I have directly refered to the matrix
[v(:,np), ip2] = func_despike_phasespace3d( vr(:,np),8, 2);
[w1(:,np), ip3] = func_despike_phasespace3d( w1r(:,np), 8, 2);
[w2(:,np), ip4] = func_despike_phasespace3d( w2r(:,np), 8, 2);
w(:,np)=(w1(:,np)+w2(:,np))/2;
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by