Trying to alter program to get duplicates from last row to last row-1

1 visualizzazione (ultimi 30 giorni)
Hi All....this program that was sent works fine for finding duplicates of the last row of values and last row-1.....moving back thro the rows,1 row at a time.
skip=1;
A1=A(1:end-skip,:); A2=A(skip+1:end,:);
M=size(A,2);
result=0;
for i=0:M-1
result=result+sum(A1==circshift(A2,[0,i]),2);
end
result
im having problems altering it,so i can count the duplicates from last row to last row-2....then moving back through the rows ,1 row at a time.....last row to last row-3....then moving back through the rows ,1 row at a time......and so on......only need from last row to last row-4
but duplicates should not be counted...between the rows......only duplicates to the last row.....if that makes sense.
Any help much appreciated
Thanks

Risposte (1)

Vedant Shah
Vedant Shah il 24 Giu 2025
Hi @ray d,
The original MATLAB code compares each row of a matrix A with the subsequent row, checking for element-wise matches across all columns, including circular shifts. This approach calculates the total number of matching elements between each row and the one that follows it, including all possible column rotations.
To compare the last row of matrix A with the four preceding rows, the following code has been provided. It ensures that only matches with the last row are considered, and not between the intermediate rows. The comparison includes all circular shifts of the columns for each of the preceding rows.
A1=A(end,:);
M =size(A,2);
result=zeros(4,1);% To store match counts for rows from last-1 to last-4
fork=1:4
ifsize(A,1)-k<1
break;% Avoid indexing before the first row
    end
A2=A(end-k,:);
result1=0;
fori=0: M -1
result1=result1+sum(A1==circshift(A2,[0,i]));
    end
result(k)= result1;
end
This implementation ensures that each of the four rows above the last is compared individually to the last row, with all possible column shifts considered. The results are stored in the result array, where each entry corresponds to the number of matching elements for one of the preceding rows.
For more information, refer to the following documentation:

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by