在此 MWE 中,我生成 dis
(节点和增量的 x、y、z 位移),然后需要单独提取位移
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
我想从 dis
构建 U
、V
、W
数组。但是,那
以下语句失败:
U(:,:)=dis{:,:}(1);
有什么简洁的方法可以进行替换吗?
最佳答案
你可以使用
U = cellfun(@(d) d(1), dis); % change index 1 to 2, 3 for V, W
或者,
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
但是,请考虑将 dis
直接定义为 3D 数组(大小为 20
×100
×3
你的例子),然后一切都会变得更容易。这里似乎不需要元胞数组。
关于matlab - 从元胞数组中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77433131/