How to separate element in row with different length?
Show older comments
I have a 3168×1 cell array matrix like this:
a =
.
.
.
[ 9611013]
[12811019]
[ 3211001]
[ 6411007]
[ 9611013]
[12811019]
[16011025]
[ 3211001]
[ 6411007]
[ 9611013]
[12811019]
[16011025]
[ 3211001]
[ 6411007]
[ 9611013]
[12811019]
[16011025]
.
.
.
I want to separate those number so i get three matrices like this
.
.
.
[ 96] [1] [1013]
[128] [1] [1019]
[ 32] [1] [1001]
[ 64] [1] [1007]
[ 96] [1] [1013]
[128] [1] [1019]
[160] [1] [1025]
[ 32] [1] [1001]
[ 64] [1] [1007]
[ 96] [1] [1013]
[128] [1] [1019]
[160] [1] [1025]
[ 32] [1] [1001]
[ 64] [1] [1007]
[ 96] [1] [1013]
[128] [1] [1019]
[160] [1] [1025]
.
.
.
I have tried
a1 = a(:,(1:5))
but i keep getting error: Index exceeds matrix dimensions. Please help me to fix this. Thank you in advance!
Answers (2)
Stephen23
on 28 Sep 2017
Two lines, no loop:
>> a = {9611013;12811019;3211001;6411007;9611013;12811019;16011025;3211001;6411007;9611013;12811019;16011025;3211001;6411007;9611013;12811019;16011025};
>> c = regexp(sprintf('%i_',a{:}),'(\d+)(\d)(\d{4})','tokens');
>> m = str2double(vertcat(c{:}))
m =
96 1 1013
128 1 1019
32 1 1001
64 1 1007
96 1 1013
128 1 1019
160 1 1025
32 1 1001
64 1 1007
96 1 1013
128 1 1019
160 1 1025
32 1 1001
64 1 1007
96 1 1013
128 1 1019
160 1 1025
Hi azurighte,
Taking the data you provided for your task, one possible solution is the following:
a = { 9611013; 12811019; 3211001; 6411007; 9611013; 12811019; 16011025; 3211001;
6411007; 9611013; 12811019; 16011025; 3211001; 6411007; 9611013; 12811019;
16011025 };
str = cellfun(@num2str,a,'UniformOutput',0);
% this part might be done more efficiently
for ik = 1:numel(str)
a2(ik,:) = [str2num(str{ik}(1:end-5)) str2num(str{ik}(end-4)) str2num(str{ik}(end-3:end))];
end
Kind regards,
Robert
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!