简体   繁体   English

如何从MATLAB中的单元格数组中的矩阵中删除某个列?

[英]How do I remove a certain column from matrices in a cell array in MATLAB?

If I have a cell array containing few matrices in it. 如果我有一个包含很少矩阵的单元格数组。 Each matrix has different row numbers but same column numbers. 每个矩阵具有不同的行号但具有相同的列号。

C{1} = [30x4 double] C{2} = [25x4 double] C{3} = [32x4 double] ...etc

If I want to remove the first and the third columns in each matrix, what should I do? 如果我想删除每个矩阵中的第一列和第三列,我该怎么办?

So the cell array will become: 所以单元格数组将成为:

new_C{1} = [30x2 double] new_C{2} = [25x2 double] new_C{3} = [32x2 double]

where those two columns in new_C are from the second and the fourth columns in the cell array C. 其中new_C中的这两列来自单元格数组C中的第二列和第四列。

I assume you have tried the obvious solution using a for loop. 我假设你已经使用for循环尝试了明显的解决方案。 Another way would be using cellfun , combined with logical indexing : 另一种方法是使用cellfun ,结合逻辑索引

columns = false(1, 4);
columns([2, 4]) = true;
D = cellfun(@(m)m(:,columns), C, 'UniformOutput', 0)

first, we build an index vector for the columns. 首先,我们为列构建索引向量。 Then we use cellfun to apply the indexing to every element in the cell array. 然后我们使用cellfun将索引应用于单元数组中的每个元素。 We use 'UniformOutput', 0 , because we want to obtain another cell array (and the results of the indexing operation are not scalar). 我们使用'UniformOutput', 0 ,因为我们想要获得另一个单元格数组(并且索引操作的结果不是标量)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM