How I convert a str to Num, and label them. please help me.

Dear Sir/Madam,
I am a beginner of MATLAB please help me, here is my prog. the below program i want to convert a string into number want a label by connecting diffident string like.
Function num_arr=convert_to_num420(str_cell_arr)
num_arr=zeros(length(str_cell_arr),1);
strs=unique(str_cell_arr);
for i=1:length(str_cell_arr)
for j=1:length(strs)
if strcmp(str_cell_arr{i},'normal.')
num_arr(i)= 1; % upto here prog is fine but next line i got error "like too many parameters"
"sir here i want to convert all the string into num. that is equal to 2" below line.
if strcmp(str_cell_arr{i},'back.','land.','neptune.','pod.','smurf.','teardrop.')
num_arr(i)= 2;
break;
else
num_arr(i)= -1;
end
end
end
end

 채택된 답변

Stephen23
Stephen23 2015년 9월 17일
편집: Stephen23 2015년 9월 17일
Perhaps you are looking for something like this:
function num_arr = convert_to_num420(str_cell_arr)
num_arr = zeros(size(str_cell_arr))-1;
num_arr(strcmp(str_cell_arr,'normal.')) = 1;
X = {'back.','land.','neptune.','pod.','smurf.','teardrop.'};
for k = 1:numel(X)
num_arr(strcmp(str_cell_arr,X{k})) = 2;
end
end
And tested:
>> convert_to_num420({'normal.'})
ans = 1
>> convert_to_num420({'back.','normal.','smurf.','false!'})
ans = 2 1 2 -1

댓글 수: 4

Thanks Sir, in my vector i have also a 3,4,5 class , how i can do? {'ftp.','guess_password.','map.','multihop.', 'phf.' , 'spy.' , 'warezclient.' , 'warezmaster.'} = 3 and {'buffer_overflow.','perl.','loadmodule.', 'rootkit.'} = 4
{'ipsweep.','nmap.', 'portsweep.','satan'} = 5
kindly tell me.
In cases like this I would create a map, either using containers.Map or simply with a cell array, between strings and numbers and use ismember for converting from string to number:
function num_arr = convert_to_num420(str_cell_arr)
map = {'normal.' 1
'back.' 2
'land.' 2
'neptune.' 2
'pod.' 2
'smurf.' 2
'teardrop.' 2};
num_arr = zeros(size(str_cell_arr))-1; %assuming you want -1 for not found
[found, index] = ismember(str_cell_arr, map(:,1));
num_arr(found) = cell2mat(map(index(found), 2));
end
It's more extensible, and the mapping a lot more obvious.
function num_arr = convert_to_num420003m(str_cell_arr) map = {'normal.' 1 'back.' 2 'land.' 2 'neptune.' 2 'pod.' 2 'smurf.' 2 'teardrop.' 2 ' ftp_write.' 3 'guess_passwd.' 3 'nmap.' 3 'multihop.' 3 'phy.' 3 'spy.' 3 'warezclient.' 3 'warezmaster.' 3 'buffer_overflow.' 4 'perl.' 4 'loadmodule.' 4 'rootkit.' 4 'ipsweep.' 5 'nmap.' 5 'portsweep.' 5 'satan.' 5}; num_arr = zeros(size(str_cell_arr))-1; %assuming you want -1 for not found [found, index] = ismember(str_cell_arr, map(:,1)); num_arr(found) = cell2mat(map(index(found), 2)); num_arr(found) = cell2mat(map(index(found), 3)); num_arr(found) = cell2mat(map(index(found), 4)); num_arr(found) = cell2mat(map(index(found), 5)); end
I want to this way , my vector like this. i run this prog, i got an error sir help me please.
Thanks Sir it is working now, i have just little bit confusion , now it if fine.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 9월 17일
편집: Walter Roberson 2015년 9월 17일
if ismember(str_cell_arr{i}, {'back.', 'land.', 'neptune.', 'pod.', 'smurf.', 'teardrop.'})

댓글 수: 4

Sir there is no convergence. i got only 0 and 1, i need normal=1 and all other string equal to 2.
What is some sample input that you are calling the routine with?
these are different attacks related to class 2. the name of class is DOS. i want to label them all is 2. str2num.
i have also post my program. plz check that.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

질문:

2015년 9월 17일

편집:

2015년 9월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by