Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 将二维数组的元素转换为单元数组_Matlab - Fatal编程技术网

Matlab 将二维数组的元素转换为单元数组

Matlab 将二维数组的元素转换为单元数组,matlab,Matlab,是否有一种方法可以立即将nxn数值数组的元素转换为nxn单元格数组,反之亦然,以便单元格数组中的每个单元格都有元素的行、列和值?例如: Input: A=[8 7 8 4 5; 7 0 7 4 4; 4 3 3 8 6; 7 0 10 8 7; 2 1 0 2 8;]; B=cell(5,5); Output: B{1

是否有一种方法可以立即将
nxn
数值数组的元素转换为
nxn
单元格数组,反之亦然,以便单元格数组中的每个单元格都有元素的行、列和值?例如:

Input:

A=[8    7     8     4     5;
  7     0     7     4     4;
  4     3     3     8     6;
  7     0    10     8     7;
  2     1     0     2     8;];

B=cell(5,5);

Output:

B{1}=[1 1 8];
B{2}=[2 1 7];
B{3}=[3 1 4];
B{4}=[4 1 7];
B{5}=[5 1 2];
B{6}=[1 2 7];
and so on...
这里有一种方法:

dim=length(A); %//square matrix
cols = repmat(1:dim,dim,1);
rows = cols';
B=reshape(num2cell([rows(:) cols(:) A(:)],2),dim,dim);
如果要经常使用,可以将这段代码封装在函数中,以便“立即”将矩阵元素转移到单元格数组。

可能使用

有趣的是,如果你使用

B=arrayfun(@(x,y,z) [x y z], row, col, A, 'uni', 0);

您得到的单元格数组
B
的大小与
A
的大小相同,其中每个元素都位于
A
中相应的元素中?喜欢不做任何工作吗?你做了什么来解决它?还有,A总是一个正方形矩阵吗?我的意思是不使用for循环,因为它对于大型矩阵来说太慢了。A和B总是方阵。你是怎么写的
B
?我在
A
的任何地方都没有看到
[11 8]
。在Matlab文档和谷歌引擎中,我找不到任何有用的东西。明白了。谢谢!
B=arrayfun(@(x,y,z) [x y z], row, col, A, 'uni', 0);