将任意大小的单元格或数组划分为定义大小的片段

时间:2015-04-28 17:01:55

标签: arrays matlab cells divide

我有一个坐标数据列表,我试图将其分成小块,以便发送到微控制器。

目前我有一个看起来很像的数据单元格:

    [46.06,32.98]
    [15.66,78,42]

我需要对值进行舍入,以便将其转换为带有cell2mat的矩阵并使用round函数。然后它看起来像有两个列:

    46 32
    15 78

MATLAB在变量窗口中将其称为(nx2)double,n为所有坐标对。

我想将这个双数组拆分成五行坐标的部分,因为我的Arduino一次只能处理这么多数据。坐标对的数量通常不是5的倍数,所以我需要用null填充其余部分。

我从那里休息了。

    Centroid = {blobSelects.Centroid}.'; %where I create the coordinate cell
    raws = cell2mat(Centroid); %I create the nx2 double
    cleans = round(raws); %I round the values

    %Here I write it to a table
    T = cell2table(cleans,'VariableNames',{'X','Y'}); 

    % and save it on a microSD card.
    dlmwrite('E:\data.txt', 'tabledata.txt', 'delimiter', '\t')    

1 个答案:

答案 0 :(得分:1)

试试这个:

A = randi([0, 100],randi(50),2); %// replace with your actual matrix
padSize = ceil(size(A,1)/5)*5 - size(A,1);
A = vertcat(A,nan(padSize,2));

out1 = mat2cell(A,ones(1,size(A,1)/5)*5,2); %// Desired cell array

如果您想将它作为3D矩阵(因为它们都具有相同的5x2大小),您可以使用

out = permute(reshape(A,5,size(A,1)/5,[]),[1 3 2]);