matlab - 从元胞数组中提取数据

标签 matlab cell-array

在此 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 构建 UVW 数组。但是,那 以下语句失败:

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/

相关文章:

string - 批量 strfind : finding lots of strings within lots of other strings

matlab - 在 MATLAB 中用元胞数组中的零替换 NaN

matlab - 脚本中的递归内联匿名函数

arrays - 如何在 MATLAB 中将元胞数组转换为数值数组?

matlab - 匿名函数中的 If-then-else

matlab - 如何确定纬度和经度是否在椭圆内

matlab - 如何连接作为 MATLAB 中元胞数组一部分的元胞数组?

python - bool numpy 数组的子矩阵求和

matlab - matlab中如何改变绘制线的颜色

matlab - 具有复数 x 的 stem(x) 的行为