arrays - 将单元格转换为 4D 数组
问题描述
我的数据以某种形式出现,但我需要另一种形式。我已经尝试过reshape
,permute
但是,我没有达到预期的结果。
输入:
A = {5 x 1} 元胞数组,其中每个元胞都是{300 x 18 single}
.
预期输出:
大小为 18 x 300 x 1 x5 的 4D 数组:
A( 1, 1,1,1) = 0.5
A( 1, 2,1,1) = 0.7
....
A( 1,300,1,1) = 0.8
...
A(18,300,1,1) = 0.99
...
...
...
A(18,300,1,5) = 0.89
(以上数值是随机的)
这是我的尝试,
z = cellfun(@(X) permute(X,[3 2 1]),A,'UniformOutput',false);
这导致
z =
5×1 cell array
{1×18×300 single}
{1×18×300 single}
{1×18×300 single}
{1×18×300 single}
{1×18×300 single}
解决方案
我认为permute
并且reshape
是要走的路。这是我的做法:
function z = q56764340(A)
if ~nargin
%% Generate some data:
A = reshape(num2cell(zeros(300,18,5,'single'), [1,2]),[], 1);
%{
A =
5×1 cell array
{300×18 single}
{300×18 single}
{300×18 single}
{300×18 single}
{300×18 single}
%}
end
%% Convert to a 4d numeric array:
z = permute( ... this does 300x18x1x5 -> 18x300x1x5
cell2mat( ... this does 1x1x1x5 -> 300x18x1x5
reshape(A,1,1,1,[])), ... this does 5x1 -> 1x1x1x5
[2,1,3,4]);
推荐阅读
- python - AWS Lambda for python 3.8 上的导入请求
- javascript - 在 p5.js 中使用 preload() 时页面卡在“正在加载...”
- apache-spark - 使用kafka流在pyspark中使用先前数据和当前数据的方法
- f# - 编译器无法识别定义的类型
- javascript - Javascript 事件更改
- c# - PUT Ajax 请求返回 404 not found asp.net MVC
- python - Python3 pickle:AttributeError:无法获取属性'CHJ_tiny_obj'
- dvc - 如何为不同的文件夹使用不同的遥控器?
- wordpress - 为什么我登出后看不到我的 WordPress 网站?
- python - 对于 Pandas 数据框中的每一行,检查行是否包含列表中的字符串