Comparing uneven sized matrix and delete rows which are not common

Hi guys. I hope this quation finds you well.
For my project i need to compare two uneven matriz row by row. If the rows are not equal beteween column 2 and 5 in a line, delete the line which de index (i+1) is equal to the other matrix index (i). If a rows is deleted, the cycle must restart. Basically i want the column 2 and 5 be exactly equal on both matrices.
For now i have this:
close all;
clc;
D=load('Dir.txt');
L=load('Esq.txt');
D=sortrows(D,6);
L=sortrows(L,6);
for i=1:length(L);
if D(i , 5) ~= L(i , 5) | D(i , 2) ~= L(i , 2)
return
end
end
But i have to manually delete the rows. Is there an option to do this automatically?
I have to delete more than 70000 rows so if i do it manually i'll be done some time next year XD.
Thanks in advance.
P.S: The files are attached

 Respuesta aceptada

Try this code
D = readmatrix('Dir.txt');
E = readmatrix('Esq.txt');
[tf, idx] = ismember(D(:, [2 5]), E(:, [2 5]), 'rows');
D_new = D(tf, :);
E_new = E(idx(idx~=0), :);
Are you looking for something like this?

6 comentarios

David Martins
David Martins el 1 de Dic. de 2020
Editada: David Martins el 1 de Dic. de 2020
Hi Ameer.
Thank you so much for replying to my question.
Infortunately is not. With this code i have a increase of data on de lower sized matrix. The data never gets increase. The final result would be two matrix with the same size and both column 2 and 5 equal.
I have attached to .JPG for a better understanding.
EDIT: I use D=sortrows(D,6); because the 6 column is relataed to time anda i need it to seperate the matrix in other small matrices.
P.S: In the .jpg files, D=D_new and L=E_new on your original code.
Can you check the following code. It will not increase the number of rows in any matrix
D = readmatrix('Dir.txt');
E = readmatrix('Esq.txt');
[tf, idx] = ismember(E(:, [2 5]), D(:, [2 5]), 'rows');
D_new = D(idx(idx~=0), :);
E_new = E(tf, :);
Also, if this is also not correct, do you want the rows in columns 2 and 5 of each matrix also to be unique. Creating two small matrices and showing the expected output will help in explaining the problem.
Still not correct.
let me try to explain better. Imagine the following matrices:
A=[2 5 7 8 6 B=[2 4 6 7 6
2 4 6 7 6 7 12 4 56 4
7 12 4 56 4 2 5 7 50 66
2 78 65 56 56 3 66 54 78 54]
3 66 54 78 54
24 78 65 56 56 ]
The rows 2 from the matrix A is equal to the row 1 form matrix B. Só i need to delete de first row from A. Than we have this:
A=[2 4 6 7 6 B=[2 4 6 7 6
23 5 54 55 66 7 12 4 56 4
3 66 54 78 54 2 5 7 50 66
7 12 4 56 4 3 66 54 78 54]
3 66 54 78 54]
Now we havo to remove de 2nd and 3rd rows of the matrix A.
A=[2 4 6 7 6 B=[2 4 6 7 6
7 12 4 56 4 7 12 4 56 4
3 66 54 78 54] 2 5 7 50 66
3 66 54 78 54]
Finally we ended with the to matrx equal to each other by removing the 3rd row of matrix B
A=[2 4 6 7 6 B=[2 4 6 7 6
7 12 4 56 4 7 12 4 56 4
3 66 54 78 54] 3 66 54 78 54]
I can do this manually but they are to many rows. to delete. I hope that example explained better my problam
EDIT: I forgot to mation that i my case only columns 2 and 5 must be equal on both matrices.
The code in my last comment does exactly this
A = [2 5 7 8 6
2 4 6 7 6
7 12 4 56 4
2 78 65 56 56
3 66 54 78 54
24 78 65 56 56];
B = [2 4 6 7 6
7 12 4 56 4
2 5 7 50 66
3 66 54 78 54];
[tf, idx] = ismember(B(:, [2 5]), A(:, [2 5]), 'rows');
A_new = A(idx(idx~=0), :);
B_new = B(tf, :);
Result
>> A_new
A_new =
2 4 6 7 6
7 12 4 56 4
3 66 54 78 54
>> B_new
B_new =
2 4 6 7 6
7 12 4 56 4
3 66 54 78 54
The problem arise where there is a repitition in the rows of B. For example, what do you expect the output for following two matrices.
A = [2 5 7 8 6
2 4 6 7 6
7 12 4 56 4
2 78 65 56 56
3 66 54 78 54
24 78 65 56 56];
B = [2 4 6 7 6
7 12 4 56 4
2 5 7 50 66
3 66 54 78 54
11 66 54 70 54];
Note that colums 2 and 5 are same for last two rows of B, but other elements are different.
You may try following code too
D = readmatrix('Dir.txt');
E = readmatrix('Esq.txt');
E = unique(E, 'rows', 'legacy');
[tf, idx] = ismember(E(:, [2 5]), D(:, [2 5]), 'rows');
D_new = D(idx(idx~=0), :);
E_new = E(tf, :);
It works!!
Thank you so much for your time an effort to enlight me on this matter!
Wish you all the best.
I am glad to be of help!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 1 de Dic. de 2020

Comentada:

el 1 de Dic. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by