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

조회 수: 6 (최근 30일)
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
Boram Lim
Boram Lim 2018년 4월 29일
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);

댓글을 달려면 로그인하십시오.

답변 (1개)

Ameer Hamza
Ameer Hamza 2018년 4월 29일
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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by