How can I split a matrix of varying size (depending on data input) into new matrices of 3 columns each?

1 view (last 30 days)
Basically I start off with a single matrix of arbitrary size (number of columns will be a multiple of 3 as data is XYZ coordinates of points) depending on the data input, and I want to split this matrix into new matrices of x rows by 3 columns each, in order to export the new matrices as single files. The issue is that the number of columns in the initial matrix will vary greatly, so I would prefer not to have to manually change the code to fit the number every data set.
Basically I want to be able to do this but without having to type out each "matrix1", etc:
matrix1 = coordinates(:,1:3);
matrix2 = coordinates(:,4:6);
matrix3 = coordinates(:,7:9);
Thanks in advance, Connor

Accepted Answer

dpb
dpb on 10 Jun 2015
Sometimes a loop is just as easy as anything else...
i1=1;
for ix=size(xyz,2)/3 % for groups of three
m=xzy(:,i1:i1+2); % your group of three
% do whatever with this set here...
...
i1=i1+3;
end
  3 Comments
dpb
dpb on 10 Jun 2015
Well, it's up to you to do whatever you want; if you don't need the subsets simultaneously, there's no point in keeping them around; if you do then you can either use a cell array or other forms to do that depending on the intended use.
Sure you can create a dynamic file name...see the FAQ How_can_I_process_a_sequence_of_files? It's written specifically to read files, but the file name creation and fopen are the same either way excepting for permission and output instead of input.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!