I just need to get the numbers from this vector. The result must be a double. I need fast and clean code, as this operation is performed over and over again in my code.

1 次查看(过去 30 天)
vector = {'G07'} {'G23'} {'G04'} {'G08'} {'G20'} {'G13'} {'G30'}
This code below is my code but it is slow when running too many times.
PRN = vector;
PRN = char(PRN);
PRN = PRN(:,2:end);
PRN = str2num(PRN);

回答(2 个)

Mathieu NOE
Mathieu NOE 2021-9-21
hello
try this alternative :
vector = [{'G07'} {'G23'} {'G04'} {'G08'} {'G20'} {'G13'} {'G30'}];
% Calling the regexp() function over the above cell array to extract number part
B = regexp(vector,'\d+(\.)?(\d+)?','match');
% Calling the str2double() function to convert the text to double-precision values
out = str2double([B{:}])

Stephen23
Stephen23 2021-9-21
编辑:Stephen23 2021-9-22
Fastest:
C = {'G07','G23','G04','G08','G20','G13','G30'};
V = sscanf(sprintf('%s',C{:}),'G%f')
V = 7×1
7 23 4 8 20 13 30

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by