How do I make different groups within a matrix?

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 Kommentare

hello
have you tried with unique ?
a = [1 1 1 2 2 2 3 3 4 4];
[C,IA,IC] = unique(a);
C =
1 2 3 4
IA =
1
4
7
9
IC =
1
1
1
2
2
2
3
3
4
4
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
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);

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Veronica Taurino
Veronica Taurino am 12 Mär. 2021
Bearbeitet: Veronica Taurino am 12 Mär. 2021

3 Stimmen

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 Kommentare

Note that in reality you may have values of z coordinates very close, that you can consider the same but due to the double data type used you will not be able to group the two as identical using (say) unique, For example
1.1234567
1.1234568
Hi Veronica,
To answer your question, these vectors I have in the matrix are surface normals I got from an stl file. I need to calculate the angle they're at and then plot that angle along a given scan path. That is why I need the z's grouped, so that I can simulate a scan path at any given z, reading the normal orientations along it. Ideally I could create different arrays whose vectors all have the same z.
Regarding your answer, is there any reason why you specified 4 z's, or is it an arbitrary number? Because I have no idea of how many different z's I actually have.
Thanks,
Jaime
Veronica Taurino
Veronica Taurino am 12 Mär. 2021
Bearbeitet: Veronica Taurino am 12 Mär. 2021
It is just a way to set equal z's coordinates over the arrays I randomly created.
If you need to group points with equal Z's, you can change the previous code as follows:
x= (50)*rand(50,1);
y= (50)*rand(50,1);
z= (50)*rand(50,1);
% I create 2 GROUPS of equal z coordinates:
% First group
z(12)=z(23);
z(34)=z(23);
z(1)=z(23);
z(45)=z(23);
z(23);
% Second group
z(1)=z(50);
z(28)=z(50);
z(11)=z(50);
z(50);
% Put all together
AA = [x,y,z]; %%%%% YOU WOULD START FROM HERE WITH YOUR DATA %%%%%%%%%
% Sort
AA_sorted=sortrows(AA,3);
% Look for index
[C,ia,ic] = unique(AA_sorted(:, 3), 'rows');
% DUPLICATE INDEX
duplicate_index2 = setdiff(1:size(AA_sorted, 1), ia);
% duplicate values
duplicate_value2 = AA_sorted(duplicate_index2, :);
% Look for index GROUP
[C3,ia3,ic3] = unique(duplicate_value2(:, 3), 'rows');
% Create group
for jj=1:length(ia3)
if jj<length(ia3)
Group{jj}=duplicate_value2(ia3(jj):ia3(jj+1)-1,:);
else
Group{jj}=duplicate_value2(ia3(jj):end,:);
end
end
In this way, I get the different groups grouped in a cell array. Be aware of my previous note and link.
Hi Veronica,
I have tried this with the data from my meshed surface and it works wonderfully. However, when instead of that I use my surface normal data (the one I need), it doesn't. For some reason, duplicate_value2 collects the vectors whose z coordinate is 0, and then the ia3 is just 1.Therefore, the loop only creates a group with all the vectores whose z coordinate is 0. Do you have any idea why this happens? May it be because all my z coordinates are between -1 and 1?
I think it may happen what I told you in the comment above, with dedicated link. Did you check it?
I can't thank you enough for this. It was very helpful!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by