How do I make different groups within a matrix?

조회 수: 9 (최근 30일)
Jaime Castiblanques
Jaime Castiblanques 2021년 3월 12일
댓글: Jaime Castiblanques 2021년 3월 15일
I have a 2904x3 matrix where each column represents the x, y and z coordinates of some vectors. Some of these vectors have the same z-coordinate and I need to group those together. Any ideas?
  댓글 수: 3
Jaime Castiblanques
Jaime Castiblanques 2021년 3월 12일
Hi Mathieu,
This allows me to have all my z organised, so thank you for that. However, I need to have each of those z values associated to their correpondent x and y values. Is there any way of organizing the z values within the 2904x3 matrix I mentioned, while keeping the x and y coordinates that go with each z value?
Thanks,
Jaime
Adam Danz
Adam Danz 2021년 3월 12일
Extending Mathieu NOE's suggestion, the 3rd output to unique is a grouping variable but you should use the stable flag to ensure that the grouping values correspond to each element of the vector.
% xyz is nx3 matrix of [x,y,z] values
[~,~,zgroup] = unique(xyz(:,3));
Alternatively, if you just want to sort the matrix according to the z column,
xyzSort = sortrows(xyz,3);

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

채택된 답변

Veronica Taurino
Veronica Taurino 2021년 3월 12일
편집: Veronica Taurino 2021년 3월 12일
How do you need to get arrays associated?
Try this:
clear all
close all
x= (50)*rand(50,1);
y= (50)*rand(50,1);
z= (50)*rand(50,1);
% I create 4 equal z coordinates:
z(12)=z(23);
z(34)=z(23);
z(1)=z(23);
z(45)=z(23);
z(23)
% Put all together
AA= [x,y,z];
% Look for index
[~, ind] = unique(AA(:, 3), 'rows');
% DUPLICATE INDEX
duplicate_index = setdiff(1:size(AA, 1), ind) % This finds the indexes of duplicates
% duplicate values
duplicate_value = AA(duplicate_index, :) % This extracts the arrays with duplicates
results:
z(23) =
12.6859 % the Z value I imposed
duplicate_index = % indexes of duplicates the script finds
12 23 34 45
duplicate_value = % array with same Z the script finds
19.6717 40.5660 12.6859
47.5017 3.6415 12.6859
11.9746 35.4336 12.6859
38.4967 19.0424 12.6859
  댓글 수: 6
Veronica Taurino
Veronica Taurino 2021년 3월 15일
I think it may happen what I told you in the comment above, with dedicated link. Did you check it?
Jaime Castiblanques
Jaime Castiblanques 2021년 3월 15일
I can't thank you enough for this. It was very helpful!

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by