Group matrix with this condition
1 view (last 30 days)
Show older comments
I have a matrix with three columns like that:
1 92.1433186490000 23.9154939160001
1 92.9534543080001 23.9122345640001
1 92.6530971670001 23.0872503970001
1 93.0291740250000 21.9333670360001
1 93.2541111270000 21.2386782580001
1 94.2215646970001 19.4112785390000
1 94.1402179610001 18.4163064250001
1 92.1429410350000 18.4263430520001
1 92.1433186490000 23.9154939160001
2 92.9534543080001 23.9122345640001
2 95.7343830470001 23.9198671710001
2 95.4631112670000 23.3802137790000
2 95.3625913070001 22.0275655970000
2 95.4085413600001 21.7528253520001
2 93.1248683680001 21.6382351500001
2 92.6530971670001 23.0872503970001
2 92.9534543080001 23.9122345640001
3 93.1248683680001 21.6382351500001
3 95.4085413600001 21.7528253520001
3 95.8866160070000 19.4626209220000
3 95.6639418050000 18.4131395020000
3 94.1402179610001 18.4163064250001
3 94.2215646970001 19.4112785390000
3 93.2541111270000 21.2386782580001
3 93.1248683680001 21.6382351500001
4 95.7343830470001 23.9198671710001
4 96.9171762360001 23.9192869440001
4 96.8640078520000 23.7995176290000
4 96.5947759490001 22.5219473650001
4 96.4500026700001 21.8299999240001
........
6 96.4500026700001 21.8299999240001
6 96.5947759490001 22.5219473650001
6 97.6237908480001 22.5663499820001
I want to obtain 6 grouped matrix with the condition that if the first column values are same. For example, one of the grouped matrix is should be like that:
a1=[92.1433186490000 23.9154939160001
92.9534543080001 23.9122345640001
92.6530971670001 23.0872503970001
93.0291740250000 21.9333670360001
93.2541111270000 21.2386782580001
94.2215646970001 19.4112785390000
94.1402179610001 18.4163064250001
92.1429410350000 18.4263430520001
92.1433186490000 23.9154939160001]
a1 belongs to values of 1.
0 Comments
Accepted Answer
Star Strider
on 2 May 2022
A = [1 92.1433186490000 23.9154939160001
1 92.9534543080001 23.9122345640001
1 92.6530971670001 23.0872503970001
1 93.0291740250000 21.9333670360001
1 93.2541111270000 21.2386782580001
1 94.2215646970001 19.4112785390000
1 94.1402179610001 18.4163064250001
1 92.1429410350000 18.4263430520001
1 92.1433186490000 23.9154939160001
2 92.9534543080001 23.9122345640001
2 95.7343830470001 23.9198671710001
2 95.4631112670000 23.3802137790000
2 95.3625913070001 22.0275655970000
2 95.4085413600001 21.7528253520001
2 93.1248683680001 21.6382351500001
2 92.6530971670001 23.0872503970001
2 92.9534543080001 23.9122345640001
3 93.1248683680001 21.6382351500001
3 95.4085413600001 21.7528253520001
3 95.8866160070000 19.4626209220000
3 95.6639418050000 18.4131395020000
3 94.1402179610001 18.4163064250001
3 94.2215646970001 19.4112785390000
3 93.2541111270000 21.2386782580001
3 93.1248683680001 21.6382351500001
4 95.7343830470001 23.9198671710001
4 96.9171762360001 23.9192869440001
4 96.8640078520000 23.7995176290000
4 96.5947759490001 22.5219473650001
4 96.4500026700001 21.8299999240001];
[Au,~,ix] = unique(A(:,1),'stable'); % May Not Be Necessary If 'Column 1' Values Are Continuous And Stepwise Monotonically Increasing, If So, Substitute 'A(:,1)' For 'ix'
Col1Nrs = accumarray(ix,1); % Tally Of 'Column 1' Occurrences
Uvals = table(Au,Col1Nrs) % Table Of Results (Optional, Requires The 'unique' Call)
Ac = mat2cell(A, Col1Nrs, 3) % Partition 'A' As Requested
Ac{1} % Results For 'Group 1'
.
More Answers (2)
Image Analyst
on 2 May 2022
Try using a logical vector to mask your N-by-3 array, m3:
a1 = m3(m3(:, 1) == 1, 2:end); % Extract only rows where column1 of m3 has the value 1.
a2 = m3(m3(:, 1) == 2, 2:end); % Extract only rows where column1 of m3 has the value 2.
a3 = m3(m3(:, 1) == 3, 2:end);
a4 = m3(m3(:, 1) == 4, 2:end);
a5 = m3(m3(:, 1) == 5, 2:end);
a6 = m3(m3(:, 1) == 6, 2:end);
2 Comments
Image Analyst
on 2 May 2022
OK, so put it in a loop
for k = 1 : whatever
% First update column 1 of m3 somehow with the new group numbers.
m3 = whatever
% Now extract the 6 groups we need for this iteration.
a1 = m3(m3(:, 1) == 1, 2:end); % Extract only rows where column1 of m3 has the value 1.
a2 = m3(m3(:, 1) == 2, 2:end); % Extract only rows where column1 of m3 has the value 2.
a3 = m3(m3(:, 1) == 3, 2:end);
a4 = m3(m3(:, 1) == 4, 2:end);
a5 = m3(m3(:, 1) == 5, 2:end);
a6 = m3(m3(:, 1) == 6, 2:end);
end
Torsten
on 2 May 2022
first_column = m3(:,1);
v = unique(first_column);
for i = 1:numel(v)
M{i} = m3(first_column == v(i),2:end)
end
0 Comments
See Also
Categories
Find more on Characters and Strings 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!