Put the leading number corresponding to the number in the column

1 Ansicht (letzte 30 Tage)
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.

Akzeptierte Antwort

Voss
Voss am 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 Kommentare
Voss
Voss am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by