Grouping values in a matrix according to a value in a row & its index position
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
JIAN-HONG YE ZHU
le 16 Mar 2023
Commenté : Dyuman Joshi
le 19 Mar 2023
Hello, say I have the following matrix:
51 71 0
48.87 63.38 -2.30769230769231
42.15 55.97 -2.30769230769231
40.22 47.47 -2.30769230769231
39.84 38.75 -1.92307692307692
40.577 34.19 -1.15384615384615
50 30 0
And I wanted to group different rows together according to equal values of the 3rd column, but index position matters, so if I have two equal values in a 3rd column but they are not next to each other in their index position, do not group together.
Which would give me:
51 71 0
48.87 63.38 -2.30769230769231
42.15 55.97 -2.30769230769231
40.22 47.47 -2.30769230769231
39.84 38.75 -1.92307692307692
40.577 34.19 -1.15384615384615
50 30 0
Would I be using the unique function? Thanks
2 commentaires
Dyuman Joshi
le 16 Mar 2023
How have you grouped the 5th and 6th rows together? The corresponding values in 3rd column are not same.
Réponse acceptée
Dyuman Joshi
le 16 Mar 2023
Modifié(e) : Dyuman Joshi
le 16 Mar 2023
Vectorized approach
mat=[51 71 0
48.87 63.38 -2.30769230769231
42.15 55.97 -2.30769230769231
40.22 47.47 -2.30769230769231
39.84 38.75 -1.92307692307692
40.577 34.19 -1.15384615384615
50 30 0];
vec=(1:size(mat,1))';
%Indices of rows in 3rd column which are not equal to the next row
%Using tolerance instead of comparing to zero
idx=vec([abs(diff(mat(:,3)))>1e-4;true])
%Corresponding number of rows in each group
elem=diff([0;idx])
%Output
out=mat2cell(mat,elem)
"what if I wanted each group to be stored in a different matrix and each value is in its corresponding column"
Any particular reason why you want to do that?
You can do that, but it is not recommended, as it inefficient and difficult to work with. Read - Why Variables should not be named Dynamically
2 commentaires
Dyuman Joshi
le 19 Mar 2023
You can do that from via the cell array as well.
mat=[51 71 0
48.87 63.38 -2.30769230769231
42.15 55.97 -2.30769230769231
40.22 47.47 -2.30769230769231
39.84 38.75 -1.92307692307692
40.577 34.19 -1.15384615384615
50 30 0];
vec=(1:size(mat,1))';
idx=vec([abs(diff(mat(:,3)))>1e-4;true]);
elem=diff([0;idx]);
%storing only 1st 2 columns
out=mat2cell(mat(:,1:2),elem);
out{2}(1,:)
out{3}
norm(out{2}(1,:)-out{3})
Plus de réponses (1)
Amit Dhakite
le 16 Mar 2023
Modifié(e) : Amit Dhakite
le 16 Mar 2023
Hi Jian-Hong,
As per my understanding, you want to group different rows based on the equal values of the 3rd column, only if these rows are next to each other.
The unique() function can indeed be used to group rows based on equal values in a specific column. However, it does not take into account the index position of the rows, and therefore it would not meet your requirement of only grouping together rows with equal values in the 3rd column if they are next to each other in their index position.
To achieve this, you can manually loop through the matrix and group the rows. The approach is to initialize an empty cell array to store the groups of rows. Then, you loop through the sorted matrix and group the rows with equal 3rd column values together. Finally, you display the groups of rows. It can be done as follows:
% Example matrix
A = [51 71 0; 48.87 63.38 -2.30769230769231; 42.15 55.97 -2.30769230769231; 40.22 47.47 -2.30769230769231; 39.84 38.75 -1.92307692307692; 40.577 34.19 -1.15384615384615; 50 30 0];
% Initialize cell array for storing groups of rows
groups = {};
% Loop through sorted matrix and group rows with equal 3rd column values together
currentGroup = [];
currentValue = A(1,3);
for i = 1:size(A,1)
if A(i,3) == currentValue
currentGroup = [currentGroup; A(i,:)];
else
groups = [groups; {currentGroup}];
currentGroup = A(i,:);
currentValue = A(i,3);
end
% if its the last row, then just add this currentGroup to the groups
% array
if i == size(A,1)
groups = [groups; {currentGroup}];
end
end
% Display the groups of rows
for i = 1:numel(groups)
disp(['Group ', num2str(i), ':']);
disp(groups{i});
end
To know more about the functions used above, kindly refer to the following links:
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!