MATLAB: How can I change the size of a structure of arrays?

조회 수: 16 (최근 30일)
Jamie Al
Jamie Al 2020년 11월 8일
댓글: Subhadeep Koley 2020년 11월 8일
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
Subhadeep Koley 2020년 11월 8일
@Lujain Almarhabi the output, what you're getting is expected for the commands you've used.
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
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.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by