Put the leading number corresponding to the number in the column

2 views (last 30 days)
Hi all,
Continue the previous discussion here but now on a different topic.
Is there a technique to put numbers based on the row position?.For example: I'm interested in putting numbers starting with number one in column 1, numbers starting with two in column 2, and so on (see red arrow below). for details can be seen in the following illustration:
for the data are:
rowcell={'10302','20245','30108','40112','52013','85911',[],[];'10268',[],'30095','40099','52014','69944','81502',[];'10308','20258','30054','40058','57019','83511',[],[];'10307','20258','30073','40077','57018','60021','70162','83511';'10325','20232','30082','40086',[],[],[],[];'10276','20228',[],'40085','52013','81531',[],[]}
or see attachment..
much appreciated help, tks great community.

Accepted Answer

Voss
Voss on 4 Jun 2022
rowcell={'10302','20245','30108','40112','52013','85911',[],[];'10268',[],'30095','40099','52014','69944','81502',[];'10308','20258','30054','40058','57019','83511',[],[];'10307','20258','30073','40077','57018','60021','70162','83511';'10325','20232','30082','40086',[],[],[],[];'10276','20228',[],'40085','52013','81531',[],[]}
rowcell = 6×8 cell array
{'10302'} {'20245' } {'30108' } {'40112'} {'52013' } {'85911' } {0×0 double} {0×0 double} {'10268'} {0×0 double} {'30095' } {'40099'} {'52014' } {'69944' } {'81502' } {0×0 double} {'10308'} {'20258' } {'30054' } {'40058'} {'57019' } {'83511' } {0×0 double} {0×0 double} {'10307'} {'20258' } {'30073' } {'40077'} {'57018' } {'60021' } {'70162' } {'83511' } {'10325'} {'20232' } {'30082' } {'40086'} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {'10276'} {'20228' } {0×0 double} {'40085'} {'52013' } {'81531' } {0×0 double} {0×0 double}
% replace empty cells with '0':
empty_idx = cellfun(@isempty,rowcell);
rowcell(empty_idx) = {'0'};
% get the first digit, to be used as column index:
idx = cellfun(@(x)x(1)-'0',rowcell);
% build result:
n_row = size(rowcell,1);
rowcell_arrange = cell(n_row,max(idx(:)));
for ii = 1:n_row
% each row of the result has the non-empty cells from that row of rowcell,
% put in column locations given by idx(ii,:) where idx(ii,:) > 0
rowcell_arrange(ii,idx(ii,idx(ii,:) > 0)) = rowcell(ii,~empty_idx(ii,:));
end
disp(rowcell_arrange)
{'10302'} {'20245' } {'30108' } {'40112'} {'52013' } {0×0 double} {0×0 double} {'85911' } {'10268'} {0×0 double} {'30095' } {'40099'} {'52014' } {'69944' } {0×0 double} {'81502' } {'10308'} {'20258' } {'30054' } {'40058'} {'57019' } {0×0 double} {0×0 double} {'83511' } {'10307'} {'20258' } {'30073' } {'40077'} {'57018' } {'60021' } {'70162' } {'83511' } {'10325'} {'20232' } {'30082' } {'40086'} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {'10276'} {'20228' } {0×0 double} {'40085'} {'52013' } {0×0 double} {0×0 double} {'81531' }
  4 Comments
Voss
Voss on 6 Jun 2022
Well, that's quite a different situation than the original question. I recommend starting a new question and maybe someone can figure it out.

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!