Group matrix with this condition

2 views (last 30 days)
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.

Accepted Answer

Star Strider
Star Strider on 2 May 2022
Using accumarray and mat2cell
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)
Uvals = 4×2 table
Au Col1Nrs __ _______ 1 9 2 8 3 8 4 5
Ac = mat2cell(A, Col1Nrs, 3) % Partition 'A' As Requested
Ac = 4×1 cell array
{9×3 double} {8×3 double} {8×3 double} {5×3 double}
Ac{1} % Results For 'Group 1'
ans = 9×3
1.0000 92.1433 23.9155 1.0000 92.9535 23.9122 1.0000 92.6531 23.0873 1.0000 93.0292 21.9334 1.0000 93.2541 21.2387 1.0000 94.2216 19.4113 1.0000 94.1402 18.4163 1.0000 92.1429 18.4263 1.0000 92.1433 23.9155
.

More Answers (2)

Image Analyst
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
busra gogen
busra gogen on 2 May 2022
Thank you! It works but I need to use for loop instead of writing a1,a2,..a6 because my column values (1,2,3...6) may change depending on my data
Image Analyst
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

Sign in to comment.


Torsten
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

Categories

Find more on Creating and Concatenating Matrices 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!