Splitting numbers of vector in multiple parts

11 visualizaciones (últimos 30 días)
Dominic Noel Kluck
Dominic Noel Kluck el 25 de Jul. de 2023
Editada: Bruno Luong el 25 de Jul. de 2023
I have a vector which can vary in length, e. g. V' = [2022024 2023074 2022044 2023014 2023054]. Now my problem is that I need to split each number into specific parts:
2022024 -> 2022 024
2023074 -> 2023 074
...
Does anyone know how to do this in a loop?
Thanks in advance

Respuesta aceptada

Bruno Luong
Bruno Luong el 25 de Jul. de 2023
Editada: Bruno Luong el 25 de Jul. de 2023
If string output is desired
V = [2022024 2023074 2022044 2023014 2023054]
V = 1×5
2022024 2023074 2022044 2023014 2023054
c = mat2cell(char(arrayfun(@num2str,V,'unif',0)),ones(length(V),1),[4 3])
c = 5×2 cell array
{'2022'} {'024'} {'2023'} {'074'} {'2022'} {'044'} {'2023'} {'014'} {'2023'} {'054'}
string(c)
ans = 5×2 string array
"2022" "024" "2023" "074" "2022" "044" "2023" "014" "2023" "054"

Más respuestas (4)

VBBV
VBBV el 25 de Jul. de 2023
Editada: VBBV el 25 de Jul. de 2023
If you want to split array of numbers, but using a loop , here's one way
V = string([2022024 2023074 2022044 2023014 2023054]);
for k = 1:length(V)
Num = char(V(k));
V_S(k,:) = [str2double(Num(1:4)) str2double(Num(5:7))];
end
V_S
V_S = 5×2
2022 24 2023 74 2022 44 2023 14 2023 54

Bruno Luong
Bruno Luong el 25 de Jul. de 2023
Editada: Bruno Luong el 25 de Jul. de 2023
If numerical value output is desired
V = [2022024 2023074 2022044 2023014 2023054]
V = 1×5
2022024 2023074 2022044 2023014 2023054
[floor(V/1000); mod(V,1000)]'
ans = 5×2
2022 24 2023 74 2022 44 2023 14 2023 54

Sachin Hegde
Sachin Hegde el 25 de Jul. de 2023
V= [2022024 2023074 2022044 2023014 2023054];
V = num2str(V);
tkn = regexp(V,'(\d+)(\d{3})','tokens');
V_split = str2double(vertcat(tkn{:}))
V_split = 5×2
2022 24 2023 74 2022 44 2023 14 2023 54

Bruno Luong
Bruno Luong el 25 de Jul. de 2023
Editada: Bruno Luong el 25 de Jul. de 2023
V = [2022024 2023074 2022044 2023014 2023054]
V = 1×5
2022024 2023074 2022044 2023014 2023054
s = string(V)';
s = [extractBefore(s,5) extractAfter(s,4)]
s = 5×2 string array
"2022" "024" "2023" "074" "2022" "044" "2023" "014" "2023" "054"

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by