In this MWE, I generate dis
(x, y, z displacements for nodes and increments), I then need to extract separately the displacements
clear
k=0;
% ignore following, this is just to buils dis
for node=1:20
for incr=1:100
k=k+1;
dis{node,incr}(1)=k; % X coordinate
dis{node,incr}(2)=2*k; % Y coordinate
dis{node,incr}(3)=3*k; % Z coordinate
end
end
I would like to build U
, V
, W
arrays from dis
. However, the
following statement fails:
U(:,:)=dis{:,:}(1);
Any concise way to make that substitution?
You can use
U = cellfun(@(d) d(1), dis); % change index 1 to 2, 3 for V, W
Alternatively,
dis_3D = cell2mat(permute(dis, [1 3 2]));
U = permute(dis_3D(:,1,:), [1 3 2]); % change index 1 to 2, 3 for V, W
However, consider defining dis
directly as a 3D array (of size 20
×100
×3
in your example), and then everything will be easier. There seems to be no need for a cell array here.