Changing Double to String Automatically in a Dataset Array
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi there,
I have a dataset array called 'data' of 2738 observations with 9 columns(2738*9) in size.
The 3rd column header is 'Surf' and contains 1's, 10's, 100's, 1000's, 10000's and 100000's.
I intend to write a for loop that goes through the data.surf column and wherever it sees a 1 it replaces it with 'F' and similarly replaces 10's with 'R' and so forth.
It seems as though I can replace the whole column with a 'F' by just typing in data.surf = 'F' but I am not quite sure how to write a mini script that automates this process. Any help or guidance will be appreciated.
Thanks in advance. Nj
댓글 수: 1
Azzi Abdelmalek
2012년 12월 26일
편집: Azzi Abdelmalek
2012년 12월 26일
Is data a struct variable? (data.surf = 'F')
채택된 답변
Teja Muppirala
2012년 12월 27일
Here are two ways to do it:
%% Method 1, using "containers.Map"
ds = dataset([10;100;10;1000;100000;1;1;10000],'VarNames','Surf');
C = containers.Map([1 10 100 1000 10000 100000],{'F' 'R' 'A' 'B' 'C' 'D'});
ds.Surf = C.values(num2cell(ds.Surf))
%% Method 2, using ISMEMBER
ds = dataset([10;100;10;1000;100000;1;1;10000],'VarNames','Surf');
C = {[1 10 100 1000 10000 100000]; {'F'; 'R'; 'A'; 'B'; 'C'; 'D'}};
[~,loc] = ismember(ds.Surf,C{1});
ds.Surf = C{2}(loc)
댓글 수: 6
Teja Muppirala
2012년 12월 27일
If you need to do be able to reverse the operation also, then I think the ISMEMBER way might be simpler. You could still do it either way though:
%% Using containers.Map
ds = dataset([10;100;10;1000;100000;1;1;10000],'VarNames','Surf');
C = containers.Map([1 10 100 1000 10000 100000],{'F' 'R' 'A' 'B' 'C' 'D'});
ds.Surf = C.values(num2cell(ds.Surf))
Cinv = containers.Map({'F' 'R' 'A' 'B' 'C' 'D'},[1 10 100 1000 10000 100000]);
ds.Surf = cell2mat(Cinv.values(ds.Surf))
%% Using ISMEMBER with an appropriate cell array
ds = dataset([10;100;10;1000;100000;1;1;10000],'VarNames','Surf');
C = {[1; 10; 100; 1000; 10000; 100000]; {'F'; 'R'; 'A'; 'B'; 'C'; 'D'}};
[~,loc] = ismember(ds.Surf,C{1});
ds.Surf = C{2}(loc)
[~,loc] = ismember(ds.Surf,C{2});
ds.Surf = C{1}(loc)
추가 답변 (3개)
Sean de Wolski
2012년 12월 26일
So something like this?
ds = dataset(rand(10,1)>0.5,'VarNames','Surf');
idx = ds.Surf;
ds.Surf = repmat('F',numel(idx),1);
ds.Surf(idx) = 'T';
ds.Surf
댓글 수: 4
Walter Roberson
2012년 12월 27일
If you are looking at data.surfs(K) then the new thing to write would be something like data.surfcodes(K)
Walter Roberson
2012년 12월 26일
letteridx = 1 + round(log10(data.surf);
letters = 'FRXUH';
data.surf = letters(letteridx);
댓글 수: 2
Walter Roberson
2012년 12월 27일
Could you show max(data.surf) before this code?
I notice that I should have allowed one more letter,
letters = 'fRSXUH';
Change the XUH to the appropriate codes for 1000, 10000, and 100000
Peter Perkins
2013년 1월 9일
Nj, you might also consider using an ordinal variable here:
>> ds = dataset([10;100;10;1000;100000;1;1;10000],'VarNames','Surf')
ds =
Surf
10
100
10
1000
1e+05
1
1
10000
>> ds.Surf = ordinal(ds.Surf,{'F' 'R' 'A' 'B' 'C' 'D'},[1 10 100 1000 10000 100000])
ds =
Surf
R
A
R
B
D
F
F
C
You can now do things like
>> ds(ds.Surf < 'A',:)
ans =
Surf
R
R
F
F
where Surf is created so that F<R<A<B<C<D (which may or my not be what you want).
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!