vec2mat w/ different number of columns

后端 未结 2 1210
礼貌的吻别
礼貌的吻别 2021-01-21 23:24

Referring to Reshape row wise w/ different starting/ending elements number @Divakar came with a nice solution but, what if the number of columns is not always the same?<

相关标签:
2条回答
  • 2021-01-21 23:52

    You could use accumarray for that:

    A = [4 9 8 9 6 1 8 9 7 7 7 4 6 2 7 1].'; %'// data
    ncols = [4 6 6]; %// columns
    n = max(ncols);
    cs = cumsum(ncols);
    ind = 1;
    ind(cs+1) = 1;
    ind = cumsum(ind(1:end-1)); %// `ind` tells the row for each element of A
    result = accumarray(ind(:), A(:), [], @(x) {[x; zeros(n-numel(x),1)]}); %// split `A` as 
        %// dictated by `ind`, and fill with zeros. Each group is put into a cell.
    result = [result{:}].'; %'// concatenate all cells
    
    0 讨论(0)
  • 2021-01-21 23:57

    You can employ bsxfun's masking capability here -

    %// Random inputs
    A = randi(9,1,15)
    ncols = [4 6 5]
    
    %// Initialize output arary of transposed size as compared to the desired 
    %// output arary size, as we need to insert values into it row-wise and MATLAB 
    %// follows column-major indexing
    out = zeros(max(ncols),numel(ncols)); 
    
    mask =  bsxfun(@le,[1:max(ncols)]',ncols); %//'# valid positions mask for output
    out(mask) = A; %// insert input array elements
    out = out.' %//'# transpose output back to the desired output array size
    

    Code run -

    A =
         5     3     7     2     7     2     4     6     8     1     9     7     5     4     5
    ncols =
         4     6     5
    out =
         5     3     7     2     0     0
         7     2     4     6     8     1
         9     7     5     4     5     0
    
    0 讨论(0)
提交回复
热议问题