convert a string vector to a vector of numbers

Hello,
suppose I have a vector of string [1 2 3 4] and i want to change it to a vector of nmbers to do a numeric caculations, How can I do it? Thank's!

댓글 수: 2

Are the '[' and ']' in the string? Are the numbers only integers or possibly floating point ?
googo
googo 2013년 4월 16일
string=['1' '2' '3' '4'] and I want to change it to : a = [1 2 3 4]

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

답변 (1개)

Friedrich
Friedrich 2013년 4월 16일
편집: Friedrich 2013년 4월 16일

0 개 추천

Hi,
as long there are no [] in the string use textscan:
textscan('1 2 3 4 6.3','%f','delimiter',' ')
In the case there are use strrep to remove the [ or ] before calling textscan.

댓글 수: 8

googo
googo 2013년 4월 16일
Why can't I use the str2num function?
thank's.
Friedrich
Friedrich 2013년 4월 16일
편집: Friedrich 2013년 4월 16일
The problem here is you dont have a delimiter. So you cant use simply textscan ( i was wrong) and also not str2num because str2num(string) would give 1234 and not [1 2 3 4]. But this should work assuming your values have one letter only:
string=['1' '2' '3' '4']
double_vec = cell2mat(textscan(sprintf('%c ',string),'%f','delimiter',' '))
Where does your string come from? Maybe its better to create the double values before your put all your characters together into one string. In that case you can use str2num.
googo
googo 2013년 4월 16일
편집: googo 2013년 4월 16일
Hey,
here is the code:
function s=ngramsFreq(string,n)
k=0;
t = repmat(char(0),length(string)-n+1,n+1);
for i=1:(length(string)-n)+1
k=k+1;
t(i,1:n)=string(k:1:(k+n)-1);
end
for i=1:(length(string)-n)+1
c=0;
for j=1:(length(string)-n)+1
if strcmp(t(i,1:n),t(j,1:n))==1
c=c+1;
t(i,n+1)=num2str(c);
end
end
end
[~,index] = unique(t,'first','rows');
q = [t(sort(index),1:n+1)];
freq=(q(1:size(q,1),n+1));
ngrams=q(1:size(q,1),1:n);
display(ngrams);
display(freq);
end
I want freq to be a vector of numbers, not a string but I changed it to string only in order to display it together with the t matrix which is a char matrix.
A simulation of the program for example:
ngramsFreq('abc abd',2)
ngrams =
ab
bc
c
a
bd
freq =
2
1
1
1
1
after I sepreated freq I want it to return to be a vector of numbers.
Friedrich
Friedrich 2013년 4월 16일
편집: Friedrich 2013년 4월 16일
You can use str2num(freq) in your case because freq is a column vector. It wouldnt work if freq is a row vector.
To see what differnce it makes if freq is column or row vector take a look at:
%row vector will give number 12
>> str2num(['1','2'])
ans =
12
%column vector will give [1; 2] as double values
>> str2num(['1';'2'])
ans =
1
2
In your post the string was a row vector, thats why I thought it gets a bit more complicated.
That code cannot handle more than 9 occurrences of the ngram.
googo
googo 2013년 4월 16일
편집: googo 2013년 4월 16일
Walter, what do you mean? I didn't understand.
You have
t(i,n+1)=num2str(c);
the destination t(i,n+1) is a single character. The number 10 and upwards require two (or more) characters to represent as a string. Therefore if c reached 10 (occurrences) the code would fail. This suggests that you have a code design problem.
googo
googo 2013년 4월 16일
Thank you... I'll try to find a way to fix it..

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

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2013년 4월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by