How can I replace numbers in a vector by text ?
이전 댓글 표시
Hi everybody!
I have a column in a csv file with 0 and 1 and I want to replace all the 0 values by FALSE and all the 1 values by TRUE. Anybody knows how can I do that? Thanks in advance!
I have been trying to do this but it doesn't work.
s(s==0)='FALSE'
답변 (1개)
dpb
2014년 6월 1일
s(s==0)='text';
won't work because you're trying to change datatypes piecewise in an array.
SOTOO,
>> s=rand(10,1)>0.5;
>> v={'t';'f'};t=v(s+1)
t =
't'
'f'
't'
't'
'f'
'f'
'f'
't'
'f'
't'
>>
댓글 수: 8
Diego
2014년 6월 1일
Diego
2014년 6월 1일
dpb
2014년 6월 1일
Say What????
You can't hold character and numeric data in the same array...if it's a cell array, it'll have to hold a column that's char and another that is numeric in the cell array as two cells.
Show in a sample code snippet what you're trying to do precisely, but sounds like it's an attempt at the impossible. Oh, there's the new datafun object that can mix metaphors if you've got a very recent version. Maybe that'd solve the problem...
Star Strider
2014년 6월 2일
Fascinated by the possibility, I did a search of the online documentation and couldn’t find any ‘datafun’ object. There’s a datafun directory but nothing else.
Where didn’t I look?
Image Analyst
2014년 6월 2일
Not sure I understand what dpb's saying. You can hold numbers and strings in a cell array and they don't need to be all the same across rows or columns. Here's proof:
a{1,1} = 1;
a{2,1} = 'Hello World!';
a{1,2} = 'MATLAB';
a{2,2} = 22
I think he was referring to a "table" which is a new object (rather than datafun), and for a table, everything in a column must be the same data type (all numbers or all strings).
dpb
2014년 6월 2일
That's what I was at least trying to say IA; you can put them in a mixed cell, but as different cell elements, not within a single cell--that doesn't work.
And, yes, I misspoke on the meaning the new table -- I don't have it so wasn't sure of it's facilities.
Star Strider
2014년 6월 2일
Oh. OK. Thought I’d been behind the door again.
Know about ‘table’ but haven’t used it much yet.
Diego
2014년 6월 2일
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!