MATLAB Answers

group in a small vector

1 view (last 30 days)
I have a vector
I need to pass this vector to a new one taking groups of 3 element so
B = [100, 111]
Does someone know how could I do this?
Thank you


Sign in to comment.

Accepted Answer

Sindar on 24 Mar 2020
Edited: Sindar on 24 Mar 2020
Ok, here is a way:
% turn A into a 1x6 string array
% reshape into a 3xN string array, then transpose to Nx3
% combine each row, leaving a Nx1 string array
% convert back to a Nx1 numeric array, then transpose to 1xN
B = double(tmp)';
or, in one line:
B = double(join(reshape(string(A),3,[])',''))'


Sindar on 24 Mar 2020
Should work for any A with a multiple of 3 elements, and error otherwise, e.g.,
Error using reshape
Product of known dimensions, 3, not divisible into total number of elements, 8.
Beatriz Dueñas Muñoz
Beatriz Dueñas Muñoz on 24 Mar 2020
thank you so much!!

Sign in to comment.

More Answers (0)

Sign in to answer this question.