필터 지우기
필터 지우기

How to Create Multidimensional table in for loop

조회 수: 5 (최근 30일)
Karthi Ramachandran
Karthi Ramachandran 2019년 4월 3일
댓글: Karthi Ramachandran 2019년 4월 9일
Trajectory =1:15;
%TrajDetail = zeros(328,4,15);
for i = 1:length(Trajectory)
TSPANtf = [ 0 t(i) ] ;
%TSPANtf2 = [ 0 t_corrected_2 ] ;
RelTol = 3.e-14 ; AbsTol = 1.e-16;
Options = odeset('RelTol',3.e-14,'AbsTol',1.e-16);
[tt,xx] = ode113(@(t,x) CRes3BP_xy(t,x,mu),TSPANtf,x0_corrected(i,1:4)',Options);
plot(xx(:,1),xx(:,2),'k',xx(:,1),-xx(:,2),'k')
pause(0.01) ;
hold on
axis equal
pos_x = xx(:,1);
pos_y = xx(:,2);
vel_x = xx(:,3);
vel_y = xx(:,4);
TrajDetail (i)= table(pos_x,pos_y,vel_x,vel_y); %here the comes as (see below the italised stuff)
end
% I have not taken the tt values out but still i need
what I wanted to do is get the values of xx and tt for each iteration and store in a table named TrajDetail 1,TrajDetail 2 ...TrajDetail 15.
Note that xx changes the row size according to "tt"
eg: Iteration 1 may cause xx to have 160 x 4 ; Iteration two may have 200 x 4 etc
table should become like Table1 (ieTrajDetail 1) containg
tt;pos_x;pos_y:vel_x;vel_y and has to be done for each iteration
Error for above
Subscripting a table using linear indexing (one subscript) or multidimensional indexing (three or more subscripts) is not supported. Use a row subscript and a variable
subscript.
  댓글 수: 1
Rik
Rik 2019년 4월 3일
Tables are 2D in Matlab, containing numbered rows and named columns. If you want something else, you can't use a table. You could use a double array to create a single large multidimensional array, or encapsulate it in a cell array.

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

채택된 답변

Rik
Rik 2019년 4월 3일
편집: Rik 2019년 4월 3일
A bold guess based on your commented code:
Trajectory =1:15;
TrajDetail = zeros(1,4,15);
for i = 1:length(Trajectory)
TSPANtf = [ 0 t(i) ] ;
%TSPANtf2 = [ 0 t_corrected_2 ] ;
RelTol = 3.e-14 ; AbsTol = 1.e-16;
Options = odeset('RelTol',3.e-14,'AbsTol',1.e-16);
[tt,xx] = ode113(@(t,x) CRes3BP_xy(t,x,mu),TSPANtf,x0_corrected(i,1:4)',Options);
plot(xx(:,1),xx(:,2),'k',xx(:,1),-xx(:,2),'k')
pause(0.01) ;
hold on
axis equal
TrajDetail (1:size(xx,1),:,i)=xx(:,1:4);
end
  댓글 수: 8
Peter Perkins
Peter Perkins 2019년 4월 9일
Rik is correct but perhaps this needs some more explanation.
Tables contain "variables", which need not be column vectors (that's why they are referred to as "variables" and not as "columns"). So if your data are 3D, NxMxP say, one way to store them is in an NxM table, each variable of which is itself NxP. Or as an NxP table with NxM variables.
Another way to store NxMxP data in a table is by flattening to 2-D as an (N*M)xP table, and then add an extra "indicator variable" to keep track of what 2nd dim slice each row corresponds to.
And finally, if all your data are numeric, just store as an NxMxP numeric matrix.
Karthi Ramachandran
Karthi Ramachandran 2019년 4월 9일
Thank you both Rik and Peter.

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

추가 답변 (1개)

Guillaume
Guillaume 2019년 4월 3일
편집: Guillaume 2019년 4월 3일
You could store your 15 tables into a cell array,
Trajectory = 1:15;
TrajDetail = cell(size(Trajectory));
for i = 1:numel(Trajectory) %I recommend using numel instead of length
%... your code as is
TrajDetail{i} = table(pos_x,pos_y,vel_x,vel_y);
end
However, you may be better of using just one table with an extra column indicating which iteration the trajectory came from:
Trajectory = 1:15
TrajDetail = [];
for i = 1:numel(Trajectory)
%... your code as is
trajindex = repmat(i, size(pos_x, 1), 1);
TrajDetail = [TrajDetail; table(trajindex, pos_x,pos_y,vel_x,vel_y)]
end
By the way, instead of
pos_x = xx(:,1);
pos_y = xx(:,2);
vel_x = xx(:,3);
vel_y = xx(:,4);
something = table(pos_x,pos_y,vel_x,vel_y);
You could just write:
something = array2table(xx, 'VariableNames', {'pos_x', 'pos_y', 'vel_x', 'vel_y'});
  댓글 수: 2
Karthi Ramachandran
Karthi Ramachandran 2019년 4월 3일
"All tables in the bracketed expression must have the same variable names." error message ,
Guillaume
Guillaume 2019년 4월 3일
I assume that error comes from my 2nd example, where I store everything in just one table. I've fixed a typo in the repmat of that code.
You certainly can't get that error from my 1st example.

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

카테고리

Help CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by