How to delete duplicate values in a column (2024)

5 Ansichten (letzte 30 Tage)

Ältere Kommentare anzeigen

Zhan am 10 Dez. 2016

  • Verknüpfen

    Direkter Link zu dieser Frage

    https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column

  • Verknüpfen

    Direkter Link zu dieser Frage

    https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column

Kommentiert: Image Analyst am 11 Dez. 2016

In MATLAB Online öffnen

Matrix A is as follows:

A = [10023 10024 10025 10026 10027

1 1 1 1 1

1 1 1 1 1

1 1 2 1 1

2 3 2 5 1

2 3 2 5 2

4 3 4 5 1

4 3 4 6 1

1 3 4 6 1

1 1 1 1 1];

I want to remove the duplicate number in each column and produce the new matrix B like following:

B = [10023 10024 10025 10026 10027

1 1 1 1 1

2 3 2 5 2

4 1 4 6 1

1 0 1 1 0];

% The zero are added to ID numbers 10024 & 10027 in order to keep the consistenty of matrix B dimension.

0 Kommentare

-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Melden Sie sich an, um diese Frage zu beantworten.

Antworten (1)

Image Analyst am 10 Dez. 2016

  • Verknüpfen

    Direkter Link zu dieser Antwort

    https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column#answer_246657

  • Verknüpfen

    Direkter Link zu dieser Antwort

    https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column#answer_246657

In MATLAB Online öffnen

This works:

A = [10023 10024 10025 10026 10027

1 1 1 1 1

1 1 1 1 1

1 1 2 1 1

2 3 2 5 1

2 3 2 5 2

4 3 4 5 1

4 3 4 6 1

1 3 4 6 1

1 1 1 1 1]

B = zeros(size(A));

for col = 1 : size(A, 2)

thisCol = A(:, col);

thisCol(diff(thisCol) == 0) = []; % Remove repeats.

B(1:length(thisCol), col) = thisCol;

end

% Trim off all zero rows

lastRow = find(all(B==0, 2), 1, 'first')-1;

B = B(1:lastRow, :)

8 Kommentare

6 ältere Kommentare anzeigen6 ältere Kommentare ausblenden

Zhan am 10 Dez. 2016

Direkter Link zu diesem Kommentar

https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column#comment_412693

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column#comment_412693

Bearbeitet: Zhan am 10 Dez. 2016

In MATLAB Online öffnen

Great!

Can you now categorize IDs based on the out of B matrix? For example:

T1 = [10023 10025

1 1

2 2

4 4

1 1

];

T2 = [10024

1

3

1

0];

T3 = [10026

1

5

6

1];

T4 = [10027

1

2

1

0];

% All IDs with sequence 1241 are added to the matrix T1 and so on...

Image Analyst am 10 Dez. 2016

Direkter Link zu diesem Kommentar

https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column#comment_412696

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column#comment_412696

Transpose B and pass it into unique() with the 'sortrows' option. Then use ismember() to find out where those rows occur in B and extract them. See if you can do it yourself.

Zhan am 11 Dez. 2016

Direkter Link zu diesem Kommentar

https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column#comment_412979

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column#comment_412979

Thanks for guiding me. I don't know how to set up ismember() function in order to extract them. Can you pls help

Image Analyst am 11 Dez. 2016

Direkter Link zu diesem Kommentar

https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column#comment_412983

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column#comment_412983

In MATLAB Online öffnen

Try

[inA, inB] = ismember(A, B);

and see what inA and inB are. Try swapping A and B. I'm sure you can figure it out.

Zhan am 11 Dez. 2016

Direkter Link zu diesem Kommentar

https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column#comment_413003

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column#comment_413003

I couldn't make it. Can you help with code ?

Image Analyst am 11 Dez. 2016

Direkter Link zu diesem Kommentar

https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column#comment_413011

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column#comment_413011

In MATLAB Online öffnen

OK, then why not simply do this

T1 = B(:, [1,3]);

T2 = B(:, 2);

T2 = B(:, 4);

T2 = B(:, 5);

Zhan am 11 Dez. 2016

Direkter Link zu diesem Kommentar

https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column#comment_413020

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column#comment_413020

But if matrix A has over 100 columns? Can you please help me on the second part of my question?

Image Analyst am 11 Dez. 2016

Direkter Link zu diesem Kommentar

https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column#comment_413038

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.mathworks.com/matlabcentral/answers/316274-how-to-delete-duplicate-values-in-a-column#comment_413038

In MATLAB Online öffnen

Well what did you try? Did you get anything like this:

A = [10023 10024 10025 10026 10027

1 1 1 1 1

1 1 1 1 1

1 1 2 1 1

2 3 2 5 1

2 3 2 5 2

4 3 4 5 1

4 3 4 6 1

1 3 4 6 1

1 1 1 1 1]

B = zeros(size(A));

for col = 1 : size(A, 2)

thisCol = A(:, col);

thisCol(diff(thisCol) == 0) = []; % Remove repeats.

B(1:length(thisCol), col) = thisCol;

end

% Trim off all zero rows

lastRow = find(all(B==0, 2), 1, 'first')-1;

B = B(1:lastRow, :)'

bRight = B(:, 2 : end)

B2 = unique(bRight, 'rows')

% Go down these rows finding out all the rows that have the row

for row = 1 : size(B2, 1)

thisRow = B2(row, :)

[ia, ib] = ismember(bRight, thisRow, 'rows')

extractedRows = B(ia, :)';

T{row} = extractedRows;

end

celldisp(T)

And you'll see:

T =

1×4 cell array

[5×1 double] [5×2 double] [5×1 double] [5×1 double]

T{1} =

10027

1

2

1

T{2} =

10023 10025

1 1

2 2

4 4

1 1

T{3} =

10024

1

3

1

T{4} =

10026

1

5

6

1

Melden Sie sich an, um zu kommentieren.

Melden Sie sich an, um diese Frage zu beantworten.

Siehe auch

Kategorien

MATLABLanguage FundamentalsData TypesCharacters and StringsString Parsing

Mehr zu String Parsing finden Sie in Help Center und File Exchange

Tags

  • matrix
  • matrix manipulation
  • array
  • cell arrays
  • matlab function

Community Treasure Hunt

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

Start Hunting!

Es ist ein Fehler aufgetreten

Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.


Translated by How to delete duplicate values in a column (11)

How to delete duplicate values in a column (12)

Website auswählen

Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .

Sie können auch eine Website aus der folgenden Liste auswählen:

Amerika

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europa

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asien-Pazifik

Kontakt zu Ihrer lokalen Niederlassung

How to delete duplicate values in a column (2024)

FAQs

How to delete duplicate values in a column? ›

To remove duplicate values, click Data > Data Tools > Remove Duplicates. To highlight unique or duplicate values, use the Conditional Formatting command in the Style group on the Home tab.

How do you remove duplicates in Excel column? ›

To remove duplicate values, click Data > Data Tools > Remove Duplicates. To highlight unique or duplicate values, use the Conditional Formatting command in the Style group on the Home tab.

How do I remove duplicates from a data frame column? ›

To remove the dataframe rows where the values in all of the specified columns are duplicated, we can use either the drop_duplicates() method or chaining groupby() and first() (or last(), or nth()). The initial dataframe remains untouched unless we modify it in place or reassign it to a variable.

How do I remove duplicates but keep one in Excel? ›

Click on the Data tab in the ribbon. In the Data Tools group, click on Remove Duplicates. In the Remove Duplicates dialog box, make sure all columns are checked and then click OK. This will remove all duplicate values from the selected range of cells, leaving only unique values.

What is the fastest way to remove duplicates in Excel? ›

Remove duplicate values

Select the range of cells that has duplicate values you want to remove. Tip: Remove any outlines or subtotals from your data before trying to remove duplicates. Select Data > Remove Duplicates, and then under Columns, check or uncheck the columns where you want to remove the duplicates.

How to remove duplicates with multiple criteria in Excel? ›

To begin with, select the range in which you want to ddelete dupes. To select the entire table, press Ctrl + A. Go to the Data tab > Data Tools group, and click the Remove Duplicates button. The Remove Duplicates dialog box will open, you select the columns to check for duplicates, and click OK.

Which keyword is used to remove duplicate values from a column? ›

One of the easiest ways to remove duplicate data in SQL is by using the DISTINCT keyword. You can use the DISTINCT keyword in a SELECT statement to retrieve only unique values from a particular column.

What are the different ways to remove duplicate values from a dataset? ›

To delete duplicates, we use a function drop_duplicates in Pandas. An argument “keep” can also be used with drop_duplicates. keep = 'first' keeps the first record and deletes the other duplicates, keep = 'last' keeps the last record and deletes the rest, and keep = False deletes all the records.

What is the shortcut to remove duplicates from a column in Excel? ›

Excel will remove the duplicate values from the selected range of cells. Using the "Alt+A+M" shortcut key to remove duplicates in Excel can save you time compared to using the menus to initiate the remove duplicates process.

How do you remove duplicates only if two columns match? ›

Removing duplicates based on two columns in Excel is easy. You can either use the Remove Duplicates Feature and select two columns, or manually combine columns and then use Remove Duplicates on that single column.

How do you highlight duplicate rows based on one column in Excel? ›

Highlight Duplicate Rows

Select the column you just filled. Go to the 'Home' tab, click 'Conditional Formatting', then 'Highlight Cells Rules', and select 'Duplicate Values'.

How to remove rows based on duplicates in one column in pandas? ›

To remove duplicates on specific column(s), use subset . To remove duplicates and keep last occurrences, use keep .

How to eliminate duplicate values based on only one column of the table in SQL? ›

To remove duplicate values based on only one column of the table in SQL, use the DELETE command along with self-join on the table. Self-Join helps in finding the duplicate values in the table, which can be deleted using the DELETE query.

References

Top Articles
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 5300

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.