Put the leading number (not sequence ) corresponding to the number in the column
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
eko supriyadi
el 7 de Jun. de 2022
Comentada: dpb
el 13 de Jun. de 2022
Dear matlab community
I've a problem with putting lead of number correspended with column. Say I've lead number like: 1, 2, 5, 55, 555, 56, 57, 58, 59, 8. In other word, i want all numbers that lead by 1 places in column 1, lead 2 in column 2, lead 5 in column 3, lead 55 in column 4, .... lead 8 ini column 10..
for easy ilustration you can see below figure:
the data:
rowcell={'10302','20245','50108','55112','58013','85911',[],[];
'10268',[],'55095','56099','58014','59944','81502',[];
'10308','20258','50054','55558','56019','83511',[],[];
'10307','20258','50073','55077','56018','56021','58162','83511';
'10325','20232','50082','55086',[],[],[],[];
'10276','20228',[],'56085','59013','81531',[],[]};
im stuck here (edited):
criteria = [{'1xxxx'},{'2xxxx'},{'5xxxx'},{'55xxx'},{'555xx'},{'56xxx'},{'57xxx'},{'58xxx'},{'59xxx'},{'8xxxx'}];
[m3,~]=size(rowcell);
n3=repmat(length(criteria),m3,1);
rowcellnew = cell(m3,max(n3));
...
tks for your help
2 comentarios
Ahmed Mansour
el 7 de Jun. de 2022
Hi , I tried to do it in a very basic way as I'm a beginner in MATLAB. Also you don't have any '57xxx' elements in the rowcell cell array.
rowcelltemp=rowcell;
criteria=cell(10,1);
criteria_index=zeros(10,1);
for i =1:48
A= rowcelltemp{i};
if(~(size(A,1)==0)&&all((A(1)=='1')))
criteria_index(1)=criteria_index(1)+1;
criteria{1,criteria_index(1)}=A;
continue;
end
if(~(size(A,1)==0)&&all((A(1)=='2')))
criteria_index(2)=criteria_index(2)+1;
criteria{2,criteria_index(2)}=A;
continue;
end
if(~(size(A,1)==0)&&all((A(1:3)=='555')))
criteria_index(5)=criteria_index(5)+1;
criteria{5,criteria_index(5)}=A;
continue;
end
if(~(size(A,1)==0)&&all((A(1:2)=='55')))
criteria_index(4)=criteria_index(4)+1;
criteria{4,criteria_index(4)}=A;
continue;
end
if(~(size(A,1)==0)&&all((A(1:2)=='56')))
criteria_index(6)=criteria_index(6)+1;
criteria{6,criteria_index(6)}=A;
continue;
end
if(~(size(A,1)==0)&&all((A(1:2)=='57')))
criteria_index(7)=criteria_index(7)+1;
criteria{7,criteria_index(7)}=A;
continue;
end
if(~(size(A,1)==0)&&all((A(1:2)=='58')))
criteria_index(8)=criteria_index(8)+1;
criteria{8,criteria_index(8)}=A;
continue;
end
if(~(size(A,1)==0)&&all((A(1:2)=='59')))
criteria_index(9)=criteria_index(9)+1;
criteria{9,criteria_index(9)}=A;
continue;
end
if(~(size(A,1)==0)&&all((A(1:2)=='8')))
criteria_index(10)=criteria_index(10)+1;
criteria{10,criteria_index(10)}=A;
continue;
end
if(~(size(A,1)==0)&&all((A(1)=='5')))
criteria_index(3)=criteria_index(3)+1;
criteria{3,criteria_index(3)}=A;
continue;
end
end
criteria=criteria';
Respuesta aceptada
eko supriyadi
el 13 de Jun. de 2022
1 comentario
dpb
el 13 de Jun. de 2022
Would have been much simpler to just clean the input instead...that would have been only a few lines of strrep calls. If didn't want to make the change permanent, wrap into another function and only fix internally to it; the original array would be unchanged.
Más respuestas (1)
dpb
el 7 de Jun. de 2022
Editada: dpb
el 7 de Jun. de 2022
criteria = [10000,20000,50000,55000,55500,56000,57000,58000,59000,80000];
rowcell={'10302','20245','50108','55112','58013','85911',[],[];
'10268',[],'55095','56099','58014','59944','81502',[];
'10308','20258','50054','55558','56019','83511',[],[];
'10307','20258','50073','55077','56018','56021','58162','83511';
'10325','20232','50082','55086',[],[],[],[];
'10276','20228',[],'56085','59013','81531',[],[]};
data=zeros(size(rowcell));
isC=cellfun(@ischar,rowcell);
data(isC)=cellfun(@str2num,rowcell(isC));
Out=nan(6,10);
for i=1:size(data,1)
ix=interp1(criteria,1:numel(criteria),data(i,:),"previous","extrap");
ix=ix(isfinite(ix));
Out(i,ix)=data(i,data(i,:)>0);
end
results in
end
>> Out
Out =
10302 20245 50108 55112 NaN NaN NaN 58013 NaN 85911
10268 NaN NaN 55095 NaN 56099 NaN 58014 59944 81502
10308 20258 50054 NaN 55558 56019 NaN NaN NaN 83511
10307 20258 50073 55077 NaN 56021 NaN 58162 NaN 83511
10325 20232 50082 55086 NaN NaN NaN NaN NaN NaN
10276 20228 NaN NaN NaN 56085 NaN NaN 59013 81531
>>
You can convert back to string or whatever form suits needs...to do with character strings is probably a fancy regexp() expression, but I'm not the one to even try it...
ADDENDUM:
Or, you can store the rowcell data into the output array as character instead of the numeric values directly if there's reason to keep that way instead --
Out=repmat({''},6,10); % array of empty cell char() strings
for i=1:size(data,1)
ix=interp1(criteria,1:numel(criteria),data(i,:),"previous","extrap");
ix=ix(isfinite(ix));
Out(i,ix)=rowcell(i,data(i,:)>0); % stuff the original char data in right column
end
which will return
>> Out
Out =
6×10 cell array
{'10302'} {'20245' } {'50108' } {'55112' } {0×0 char} {0×0 char} {0×0 char} {'58013' } {0×0 char} {'85911' }
{'10268'} {0×0 char} {0×0 char} {'55095' } {0×0 char} {'56099' } {0×0 char} {'58014' } {'59944' } {'81502' }
{'10308'} {'20258' } {'50054' } {0×0 char} {'55558' } {'56019' } {0×0 char} {0×0 char} {0×0 char} {'83511' }
{'10307'} {'20258' } {'50073' } {'55077' } {0×0 char} {'56021' } {0×0 char} {'58162' } {0×0 char} {'83511' }
{'10325'} {'20232' } {'50082' } {'55086' } {0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char}
{'10276'} {'20228' } {0×0 char} {0×0 char} {0×0 char} {'56085' } {0×0 char} {0×0 char} {'59013' } {'81531' }
>>
NB: your original rowcell as defined will contain 0x0 double elements instead so you'll have mixed types to deal with when addressing.
The above is all char in cells; it could then actually be turned into a newfangled string array that just might be handy form to have...
>> string(Out)
ans =
6×10 string array
"10302" "20245" "50108" "55112" "" "" "" "58013" "" "85911"
"10268" "" "" "55095" "" "56099" "" "58014" "59944" "81502"
"10308" "20258" "50054" "" "55558" "56019" "" "" "" "83511"
"10307" "20258" "50073" "55077" "" "56021" "" "58162" "" "83511"
"10325" "20232" "50082" "55086" "" "" "" "" "" ""
"10276" "20228" "" "" "" "56085" "" "" "59013" "81531"
>>
Just all depends upon what the whole point of the exercise is down the road...
2 comentarios
eko supriyadi
el 7 de Jun. de 2022
Editada: eko supriyadi
el 7 de Jun. de 2022
dpb
el 7 de Jun. de 2022
Couple of issues in that dataset doesn't match the assumptions made based on the example --
- It contains both [0x0 char] and [0x0 double] elements; the first step presumes all empty cells are the double;
- More troublesome is there are values that cannot be converted to numeric --
Working around the first, we change to look for empty and return "isNE", the indices that aren't empty--
isNE=~cellfun(@isempty,rowcell);
we then look for those elements that aren't convertible to numeric form --
isNNum=~cellfun(@(c)matches(c,digitsPattern),rowcell(isNE));
and select those to see what gives -- this is a two-step process; there's no syntax in MATLAB to "stack" indices
rcNE=rowcell(isNE);
rcNE(isNNum)
The result of the above is the following set--
>> rcNE(isNNum)
ans =
14×1 cell array
{'5796/'}
{'5796/'}
{'5790/'}
{'809//'}
{'809//'}
{'809//'}
{'809//'}
{'809//'}
{'809//'}
{'809//'}
{'809//'}
{'809//'}
{'809//'}
{'809//'}
>>
So, whassup w/ these? I presume it means some form of freeform number, but won't be able to use those for the lookup table approach with interp1 (which is a very neat trick, btw -- I've used the reverse lookup as here quite a number of times).
I've got meeting in town have to get cleaned up for and head that way, looks like you need to do some preprocessing on your data before applying the above; turn all the empty cells into one or the other of double or empty string; you'll have to deal with that dichotomy at some point or the other; either will/can be made to work, but handling both simultaneously will be a problem; I'd go w/ the double() because the later step is to select only the nonzero elements for the lookup; that's not so convenient if they're not numeric.
The second is to turn all the above into convertible numbers -- replacing the slashes with zeros would be the logical conversion.
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!