Divide dataset into N parts, remove vectors (or Matrix) with NaN and store all subdata into one list or array

4 views (last 30 days)
I recently switched to Matlab from R so, I am a beginner of Matlab. I have a very large dataset and below is the sample, MM2, for it. With the data MM2, I would like to divide MM2 into a unique number of MM1 (here is two, let's say N in my data) and remove all vectors of NaN for each of N subdata(there are two subdata below) and store each of subdata into one large array or something.
MM2 =
MM1 MM2 MM3 MM4 MM5 MM6
1 3 0 NaN NaN 1
1 3 3 NaN NaN 5
1 1 5 NaN NaN 7
1 3 5 NaN NaN 15
1 1 9 NaN NaN 1
1 1 10 NaN NaN 1
1 4 12 NaN NaN 5
1 2 13 NaN NaN 3
1 4 15 NaN NaN 5
1 2 16 NaN NaN 6
2 3 NaN NaN 1 0
2 3 NaN NaN 5 3
2 1 NaN NaN 7 5
2 3 NaN NaN 15 5
2 1 NaN NaN 1 9
2 1 NaN NaN 1 10
2 4 NaN NaN 5 12
2 2 NaN NaN 3 13
2 4 NaN NaN 5 15
2 2 NaN NaN 6 16
Tha is, I would like to have two data sets
First =
MM1 MM2 MM3 MM6
1 3 0 1
1 3 3 5
1 1 5 7
1 3 5 15
1 1 9 1
1 1 10 1
1 4 12 5
1 2 13 3
1 4 15 5
1 2 16 6
Second =
MM1 MM2 MM5 MM6
2 3 1 0
2 3 5 3
2 1 7 5
2 3 15 5
2 1 1 9
2 1 1 10
2 4 5 12
2 2 3 13
2 4 5 15
2 2 6 16
And, I would like to store N subdata into one large array, list or whatever (I don't know what is the right one, list in program R). How should I do it?
  1 Comment
Boram Lim
Boram Lim on 29 Apr 2018
For reproduce, use this
L1 = 10; A0 = ones([L1,1]); A1 = [3;3;1;3;1;1;4;2;4;2;]; A2 = [0;3;5;5;9;10;12;13;15;16;]; A3 = [1;5;7;15;1;1;5;3;5;6;]; A4 = NaN([L1,1]) ;
MM = [A0,A1,A2,A4,A4,A3; A0*2,A1,A4,A4,A3,A2];
MM2 = mat2dataset(MM);

Sign in to comment.

Answers (1)

Ameer Hamza
Ameer Hamza on 29 Apr 2018
The following code will divide the dataset as you specified in the question.
MM2table = dataset2table(MM2);
MM2mat = MM2table{:, :};
groups = splitapply(@(MM) {mat2dataset(MM)}, MM2mat(:,1:end), findgroups(MM2mat(:,1)));
for i=1:length(groups)
group2mat = double(groups{i});
nanIndex = isnan(group2mat);
nanColumn = all(nanIndex);
groups{i}(:, nanColumn) = [];
end

Tags

Products

Community Treasure Hunt

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

Start Hunting!