How to Create Multidimensional table in for loop

2 visualizaciones (últimos 30 días)
Karthi Ramachandran el 3 de Abr. de 2019
Comentada: Karthi Ramachandran el 9 de Abr. de 2019
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 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Rik el 3 de Abr. de 2019
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.

Iniciar sesión para comentar.

Rik el 3 de Abr. de 2019
Editada: Rik el 3 de Abr. de 2019
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 comentariosMostrar 6 comentarios más antiguosOcultar 6 comentarios más antiguos
Peter Perkins el 9 de Abr. de 2019
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 el 9 de Abr. de 2019
Thank you both Rik and Peter.

Iniciar sesión para comentar.

Más respuestas (1)

Guillaume el 3 de Abr. de 2019
Editada: Guillaume el 3 de Abr. de 2019
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
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)
trajindex = repmat(i, size(pos_x, 1), 1);
TrajDetail = [TrajDetail; table(trajindex, pos_x,pos_y,vel_x,vel_y)]
end
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 comentariosMostrar NingunoOcultar Ninguno
Karthi Ramachandran el 3 de Abr. de 2019
"All tables in the bracketed expression must have the same variable names." error message ,
Guillaume el 3 de Abr. de 2019
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.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB Compiler en Help Center y File Exchange.

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by