MATLAB: How can I change the size of a structure of arrays?
    6 ビュー (過去 30 日間)
  
       古いコメントを表示
    
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 件のコメント
  Subhadeep Koley
    
 2020 年 11 月 8 日
				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
回答 (1 件)
  Walter Roberson
      
      
 2020 年 11 月 8 日
        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 件のコメント
参考
カテゴリ
				Help Center および File Exchange で Cell Arrays についてさらに検索
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


