Grouping values in a matrix according to a value in a row & its index position
20 次查看(过去 30 天)
显示 更早的评论
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 个评论
Dyuman Joshi
2023-3-16
How have you grouped the 5th and 6th rows together? The corresponding values in 3rd column are not same.
采纳的回答
Dyuman Joshi
2023-3-16
编辑:Dyuman Joshi
2023-3-16
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 个评论
Dyuman Joshi
2023-3-19
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})
更多回答(1 个)
Amit Dhakite
2023-3-16
编辑:Amit Dhakite
2023-3-16
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:
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!