How to compare part of a table row with other rows?

3 views (last 30 days)
Hello,
I have a table with 10 columns. The 10th column has just zeros. I want to check if the first eight columns in each row are equal and give them the same sequential number if the rows are equal. The sequential numbers should be in the 10th column. The 9th column is uninteresting.
For example:
T = [
5 5 5 5 2 0 0 0 -45 0
15 5 5 5 2 0 0 0 -45 0
35 15 25 5 2 0 0 0 45 0
5 5 5 5 2 0 0 0 45 0
15 5 5 5 2 0 0 0 45 0
35 15 15 5 6 0 0 0 45 0
So I want that row one and three get a one and row two and five get a two and so on.
T = [
5 5 5 5 2 0 0 0 -45 1
15 5 5 5 2 0 0 0 -45 2
35 15 25 5 2 0 0 0 45 0
5 5 5 5 2 0 0 0 45 1
15 5 5 5 2 0 0 0 45 2
35 15 15 5 6 0 0 0 45 0]
T = 6×10
5 5 5 5 2 0 0 0 -45 0 15 5 5 5 2 0 0 0 -45 0 35 15 25 5 2 0 0 0 45 0 5 5 5 5 2 0 0 0 45 0 15 5 5 5 2 0 0 0 45 0 35 15 15 5 6 0 0 0 45 0
T = 6×10
5 5 5 5 2 0 0 0 -45 1 15 5 5 5 2 0 0 0 -45 2 35 15 25 5 2 0 0 0 45 0 5 5 5 5 2 0 0 0 45 1 15 5 5 5 2 0 0 0 45 2 35 15 15 5 6 0 0 0 45 0
I do not know how to start.

Accepted Answer

Voss
Voss on 17 Nov 2022
T = [
5 5 5 5 2 0 0 0 -45 0
15 5 5 5 2 0 0 0 -45 0
35 15 25 5 2 0 0 0 45 0
5 5 5 5 2 0 0 0 45 0
15 5 5 5 2 0 0 0 45 0
35 15 15 5 6 0 0 0 45 0];
[~,~,jj] = unique(T(:,1:8),'rows','stable')
jj = 6×1
1 2 3 1 2 4
T(:,10) = jj
T = 6×10
5 5 5 5 2 0 0 0 -45 1 15 5 5 5 2 0 0 0 -45 2 35 15 25 5 2 0 0 0 45 3 5 5 5 5 2 0 0 0 45 1 15 5 5 5 2 0 0 0 45 2 35 15 15 5 6 0 0 0 45 4
  2 Comments
Voss
Voss on 17 Nov 2022
Edited: Voss on 17 Nov 2022
The example matrix T in the question had 10 columns including the new "number" column (and you don't care about column 9), but the T in this code has 9 columns including the new "number" column (and I assume you don't care about column 8).
So you would sortrows with just the first 7 columns in this case (or maybe in general, use columns 1:end-2):
r_s = [ 5 15 35];
alpha = [ 5 15 25];
beta_0 = [ 5 15 25];
beta_2 = [ 5 15 25];
r_a = [ 2 6 10];
lambda = [-45 45];
CR_2 = [0.0 0.25 0.5];
CR_0 = [0.0 0.25 0.5];
levels=[length(r_s), length(alpha), length(beta_0), length(beta_2), length(r_a), length(CR_2), length(CR_0), length(lambda)];
DOE = fullfact(levels);
DOE(:,1)=r_s(DOE(:,1));
DOE(:,2)=alpha(DOE(:,2));
DOE(:,3)=beta_0(DOE(:,3));
DOE(:,4)=beta_2(DOE(:,4));
DOE(:,5)=r_a(DOE(:,5));
DOE(:,6)=CR_2(DOE(:,6));
DOE(:,7)=CR_0(DOE(:,7));
DOE(:,8)=lambda(DOE(:,8));
T = array2table(DOE, 'VariableNames',{'r_s','alpha','beta_0','beta_2','r_a', 'CR_2', 'CR_0','lambda'});
T.No = zeros(4374,1);
T = table2array(T) % 9 columns
T = 4374×9
5 5 5 5 2 0 0 -45 0 15 5 5 5 2 0 0 -45 0 35 5 5 5 2 0 0 -45 0 5 15 5 5 2 0 0 -45 0 15 15 5 5 2 0 0 -45 0 35 15 5 5 2 0 0 -45 0 5 25 5 5 2 0 0 -45 0 15 25 5 5 2 0 0 -45 0 35 25 5 5 2 0 0 -45 0 5 5 15 5 2 0 0 -45 0
[~,~,jj] = unique(T(:,1:end-2),'rows','stable');
T(:,end) = jj;
% check the beginning and where the numbers should start repeating:
T([1 2 3 2188 2189 2190],:)
ans = 6×9
5 5 5 5 2 0 0 -45 1 15 5 5 5 2 0 0 -45 2 35 5 5 5 2 0 0 -45 3 5 5 5 5 2 0 0 45 1 15 5 5 5 2 0 0 45 2 35 5 5 5 2 0 0 45 3

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!