填充零后如何使单元格长度相同 [英] How to make cell array same length after padding zeros
问题描述
这是具有2 * 159单元格的数据矩阵 在数据矩阵中,有许多列(2个向量)具有不同的长度.我想填充具有最小长度的零.零数应为
This is data matrix having 2*159 cells In the data matrix so many columns ( 2 vectors) having different length . I want to pad zeros which have the minimum length. Number of zeros should be
[max(length(Vector 1))-min(length(Vector 2))]
现在,我想在单元格中添加长度最小的零.如果列的长度没有问题,并且希望使每一列的长度相等.
Now I want add zeros in the cell which have minimum length. and want to make length equal in each column if column have same length than no problem.
推荐答案
n = max(max(cellfun(@(x)size(x,2),data)))
cellfun(@(x)[x, zeros(1,n-numel(x))], data, 'uni', 0)
以上内容是找到一个单个最大值来填充单元格.现在,我意识到您实际上希望以每列为基础计算该最大值:
The above was finding one single max value to pad the cells to. I realise now that you actually want this max calculated on a per column basis:
n = max(cellfun(@(x)size(x,2),data))
cellfun(@(x,n)[x, zeros(1,n-numel(x))], data, repmat(num2cell(n),2,1), 'uni', 0)
此时,仅使用循环可能更直观
At this point it might be more intuitive to just use a loop
for col = 1:size(data,2)
n = max(size(data{1,col},2),size(data{2,col},2));
for row = 1:2
x = data{row,col};
data{row,col} = [x, zeros(1,n-numel(x))];
end
end
这篇关于填充零后如何使单元格长度相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!