MATLAB: How can I change the size of a structure of arrays?
10 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have the following the structure of arrays:
ray_state_vec_in2.r = ray_state_vec.r(:,[1:3]);
ray_state_vec_in2.Q = ray_state_vec.Q(:,[1:3]); %.Q(1)
ray_state_vec_in2.theta = ray_state_vec.theta(:,[1:3]); %theta(1)
ray_state_vec_in2.delta_r = ray_state_vec.delta_r(:,[1:3]);
ray_state_vec_in2.delta_Q = ray_state_vec.delta_Q(:,[1:3]);
ray_state_vec_in2.deviative_absorption = ...
ray_state_vec.deviative_absorption(:,[1:3]);
ray_state_vec_in2.phase_path = ray_state_vec.phase_path(:,[1:3]);
ray_state_vec_in2.group_path = ray_state_vec.group_path(:,[1:3]);
ray_state_vec_in2.group_step_size = ray_state_vec.group_step_size(:,[1:3]);
However, if I look up the size of ray_state_vec_in2.r and similarly for .Q, .theta and so on:
size(ray_state_vec_in2.r)
ans =
1 3
But, somehow the size of ray_state_vec_in2
size(ray_state_vec_in2)
ans =
1 1
How can I get a size of 1x3 for ray_state_vec_in2 ? Is the way I define the structure of arrays incorrect?
1 Commento
Subhadeep Koley
il 8 Nov 2020
However to achieve 1X3 ray_state_vec_in2, you can use the code below
ray_state_vec_in2 = struct([]);
for idx = 1:3
ray_state_vec_in2(idx).r = ray_state_vec.r(:, idx);
ray_state_vec_in2(idx).Q = ray_state_vec.Q(:, idx);
ray_state_vec_in2(idx).theta = ray_state_vec.theta(:, idx);
ray_state_vec_in2(idx).delta_r = ray_state_vec.delta_r(:, idx);
ray_state_vec_in2(idx).delta_Q = ray_state_vec.delta_Q(:, idx);
ray_state_vec_in2(idx).deviative_absorption = ray_state_vec.deviative_absorption(:, idx);
ray_state_vec_in2(idx).phase_path = ray_state_vec.phase_path(:, idx);
ray_state_vec_in2(idx).group_path = ray_state_vec.group_path(:, idx);
ray_state_vec_in2(idx).group_step_size = ray_state_vec.group_step_size(:, idx);
end
Risposte (1)
Walter Roberson
il 8 Nov 2020
struct2cell(ray_state_vec)
and cellfun num2cell to break the arrays up into cell array, one per column. This will get you a cell array with one entry per field, and each field will have three cells, one per column. vertcat() the expansion of that cell, like vertcat(Temporary{:}) to get a cell array with one row per field and one column for each of the three data columns. Now you can cell2struct() to put it all back together into a struct array.
Or... loop.
0 Commenti
Vedere anche
Categorie
Scopri di più su Structures in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!