Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

changing the values

Asked by FIR

FIR (view profile)

on 25 Feb 2012
Latest activity Edited by Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 25 Oct 2013

i have values in

r as

'<=50k' '>50k' '>50k' ; ;

; '>50k'

now i want to replace these values

>50k as 1

<=50 as 0

please help

i read those values from excel sheet

[d r]=xlsread('mydata.xls');in whicg i got those values in r



FIR (view profile)



No products are associated with this question.

1 Answer

Answer by Andrei Bobrov

Andrei Bobrov (view profile)

on 25 Feb 2012
Accepted answer

in this case

r = {'<=50k' '>50k' '>50k' '>50k'}
[a n n] = unique(r)
k = {0,1}
rnew = k(n)


rnew = cellfun(@(x)x-'0',regexprep(r,{'<=50k' ,   '>50k'},{'0','1'}))

on FIR comment: try

rnew = regexprep(r,{'<=50k' ,   '>50k'},{'0','1'})

on Pat comment

r = {'<=50k' '>50k' '>50k' '>50k' 'fg' ''}
rnew = regexprep(r, {'<=50k', '>50k'}, {'0', '1'})
t = ismember(rnew, {'0', '1'})
rnew(t) = cellfun(@(x)x-'0', rnew(t), 'un', 0)



FIR (view profile)

on 25 Feb 2012

Andrei error in both

in first i get values more than 1

and in second is get error aS

Error using ==> cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.


Pat (view profile)

on 25 Feb 2012

it works,but i want that single quotes to be removed for further processing

Andrei Bobrov

Andrei Bobrov (view profile)

Contact us