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 commentaires

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

Connectez-vous pour commenter.

Réponses (1)

Friedrich
Friedrich le 16 Avr 2013
Modifié(e) : Friedrich le 16 Avr 2013
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 commentaires

googo
googo le 16 Avr 2013
Why can't I use the str2num function?
thank's.
Friedrich
Friedrich le 16 Avr 2013
Modifié(e) : Friedrich le 16 Avr 2013
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 le 16 Avr 2013
Modifié(e) : googo le 16 Avr 2013
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 le 16 Avr 2013
Modifié(e) : Friedrich le 16 Avr 2013
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 le 16 Avr 2013
Modifié(e) : googo le 16 Avr 2013
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 le 16 Avr 2013
Thank you... I'll try to find a way to fix it..

Connectez-vous pour commenter.

Catégories

Tags

Aucun tag saisi pour le moment.

Question posée :

le 16 Avr 2013

Community Treasure Hunt

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

Start Hunting!

Translated by