How to Create Multidimensional table in for loop
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Karthi Ramachandran
le 3 Avr 2019
Commenté : Karthi Ramachandran
le 9 Avr 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 commentaire
Rik
le 3 Avr 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.
Réponse acceptée
Rik
le 3 Avr 2019
Modifié(e) : Rik
le 3 Avr 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 commentaires
Peter Perkins
le 9 Avr 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.
Plus de réponses (1)
Guillaume
le 3 Avr 2019
Modifié(e) : Guillaume
le 3 Avr 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
%... 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 commentaires
Guillaume
le 3 Avr 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.
Voir également
Catégories
En savoir plus sur MATLAB Compiler dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!